首页 >Java >在 if 语句上打印成功金额语句

在 if 语句上打印成功金额语句

PHPz
PHPz转载
2024-02-06 10:39:071062浏览
问题内容

我用java制作了一个atm程序。总的来说,它有效。但是,当用户输入成功的储蓄和支票金额时,我试图让它打印“您超出了限制”。我使用 if 语句来做到这一点。但是,我不确定它是否可以生成“您超出了限制”打印。我刚开始学习 java,所以请耐心等待。

package atm;

import java.util.Scanner;

public class atm {

    public static void main(String[] args) {
        
        Scanner cin = new Scanner(System.in);
        
        int checking = 1000, savings = 5000, withdrawal = 0, deposit = 0;
        
        System.out.println("======================");
        
        System.out.println("Welcome to Sound Bank!");
        
        System.out.println("======================");
        
        while (true) {
        
        System.out.println("Please select which options: \n (C)Checking Balance \n (S)Savings");
        
        String choice1 = cin.nextLine(); 
        
        System.out.println("Please select of the following: ");
        
        System.out.println("(D)Deposit \n (W)Withdrawal \n (B)Check Balance \n (E)Exit");
        
        String choice2 = cin.nextLine(); 
        
        
            switch(choice2.toUpperCase()) {
            
            case "D":
                
                if (choice1.equalsIgnoreCase("C")) {
                    System.out.println("Enter your deposit amount:");
                    double num = cin.nextDouble();
                    double total = checking + num;
                    System.out.println("You entered" + num);
                    System.out.println("You have" + total);
                        if(num > checking) {
                            System.out.println("You are over the limit.");
                        }
                } else if (choice1.equalsIgnoreCase("S")) {
                    System.out.println("Enter your deposit amount");
                    double num1 = cin.nextDouble();
                    double total1 = savings + num1;
                    System.out.println("You entered" + num1);
                    System.out.println("You have" + total1);
                        if(num1 > savings) {
                            System.out.println("You are over the limit.");
                        }
                } 
                break;
            case "W":
                if (choice1.equalsIgnoreCase("C")) {
                    System.out.println("Enter your withdrawal amount:");
                    double w = cin.nextDouble();
                    double total2 = checking - w;
                    System.out.println("You entered" + w);
                    System.out.println("You have" + total2);
                        if(w > checking) {
                            System.out.println("You are over the limit.");
                        }
                } else if (choice1.equalsIgnoreCase("S")) {
                    System.out.println("Enter your withdrawal amount:");
                    double w2 = cin.nextDouble();
                    double total3 = cin.nextDouble();
                    System.out.println("You entered " + w2);
                    System.out.println("You have" + total3);
                        if(w2 > savings) {
                            System.out.println("You are over the limit.");
                        }
                } 
                break;
            case "B": 
                if (choice1.equalsIgnoreCase("C")) {
                    double t1 = (checking + deposit) - withdrawal;
                    System.out.println("Your total amount is:" + t1);
                } else if (choice1.equalsIgnoreCase("S")) {
                    double t2 = (savings + deposit) - withdrawal;
                    System.out.println("Your total amount is:" + t2);
                }
                break;
            case "E": 
                System.exit(0);
                break;
            }
        
    
        }

    }

}

正确答案


问题不是很清楚。但我假设你的问题已经解决了

double w2 = cin.nextDouble();

double Total3 = cin.nextDouble();

替换为

double w2 = cin.nextDouble();

double Total3 = 节省 - w2;

它应该按预期工作。

以上是在 if 语句上打印成功金额语句的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文转载于:stackoverflow.com。如有侵权,请联系admin@php.cn删除