如何在开关盒中保存数量输入?

时间:2015-08-02 10:30:16

标签: java switch-statement

我的收银程序出了问题,我想要求用户输入一些数量,只限于4。

我目前的问题是:

我应该如何修复我的程序,以便即使我的命令仍在循环,用户也只能输入最多4个。

无论如何,这是我的代码。

public static void main(String[] args) {
    Scanner scan =  new Scanner(new InputStreamReader(System.in,Charset.defaultCharset()));;
    Scanner choiz = new Scanner(System.in);
    Scanner pay = new Scanner(System.in);
    double total = 0;

    double totalA = 0;
    double totalB = 0;
    double totalC = 0;
    double totalD = 0;
    double totalE = 0;

    double A = 3.25;
    double B = 5.00;
    double C = 3.00;
    double D = 1.50;
    double E = 4.50;

    char choice;
    char choice2;
    double payment = 0;
    double change = 0;
    int numb;
    do {
        System.out.println("|------------------------------------|");
        System.out.println("|                MENU                |");
        System.out.println("|------------------------------------|");
        System.out.println("| Press   |          Product         |");
        System.out.println("|------------------------------------|");
        System.out.println("| A       | Product A        3.25    |");
        System.out.println("| B       | Product B        5.00    |");
        System.out.println("| C       | Product C        3.00    |");
        System.out.println("| D       | Product D        1.50    |");
        System.out.println("| E       | Product E        4.50    |");
        System.out.println("|------------------------------------|");
        System.out.println("| R       |          Receipt         |");
        System.out.println("|------------------------------------|");
        System.out.println();
        System.out.println("Enter Choice:");

        choice = choiz.next().toUpperCase().charAt(0);
        switch (choice) {
            case 'A':
                System.out.println("Item A Selected!");
                System.out.println("Please enter number of item/s: ");
                //numb = scan.nextInt();
                while (!scan.hasNextInt()) {
                    scan.next();
                    System.out.print("Please enter an integer: ");
                }
                numb = scan.nextInt();
                if (numb <= 4) {
                    totalA = numb * A;
                    System.out.println("Current Total: " + totalA);
                }
                else {
                    System.out.println("Only accepting 4 items and below!");
                }
                break;
            case 'B':
                System.out.println("Item B Selected!");
                System.out.println("Please enter number of item/s: ");
                numb = scan.nextInt();                                                 
                if (numb <= 4) {
                    totalB = numb*B;
                    System.out.println("Current Total: " + totalB);
                }
                else {
                    System.out.println("Only accepting 4 items and below!");
                }
                break;   
            case 'C':
                System.out.println("Item C Selected!");
                System.out.println("Please enter number of item/s: ");
                numb = scan.nextInt();
                if (numb <= 4) {
                    totalC = numb * C;
                    System.out.println("Current Total: " + totalC);
                }
                else {
                    System.out.println("Only accepting 4 items and below!");
                }
                break;    
            case 'D':
                System.out.println("Item D Selected!");
                System.out.println("Please enter number of item/s: ");
                numb = scan.nextInt();
                if (numb <= 4) {
                    totalD = numb * D;
                    System.out.println("Current Total: " + totalD);
                }
                else {
                    System.out.println("Only accepting 4 items and below!");
                }
                break; 
            case 'E':
                System.out.println("Item E Selected!");
                System.out.println("Please enter number of item/s: ");
                numb = scan.nextInt();
                if (numb <= 4) {
                    totalE = numb * E;
                    System.out.println("Current Total: " + totalE);
                }
                else {
                    System.out.println("Only accepting 4 items and below!");
                }
                break;    
            case 'R':
                System.out.println("=====================================");
                System.out.println("|       Receipt for purchase         |");
                System.out.println("=====================================");
                break;
            default:
                System.out.println("Invalid selection!");    
        }
    }
    while (choice != 'R');
        total = totalA + totalB + totalC + totalD + totalE;
        System.out.println("You purchased the following:");
        System.out.println("Item A \t" + "Amount: " + totalA);
        System.out.println("Item B \t" + "Amount: " + totalB);
        System.out.println("Item C \t" + "Amount: " + totalC);
        System.out.println("Item D \t" + "Amount: " + totalD);
        System.out.println("Item E \t" + "Amount: " + totalE);
        System.out.println("\t\t=====================================");
        System.out.println("\t\t| Total bill to pay: " + total + "|");
        System.out.println("\t\t=====================================");

        System.out.println("Enter your Payment: ");
        payment = pay.nextInt();

              if(payment > total || payment == total){
              change = payment - total;
                  System.out.println(" Your Payment is " + payment + " and your change is " + change);
                  System.out.println("[---Thank You for Ordering---]");
              }
              else{
                  do{
              System.out.println("Insufficient amount of Payment!");
              System.out.println("Press [Y] to Try again or [N] to Exit: ");
              choice2 = choiz.next().toUpperCase().charAt(0);

                  switch(choice2)
                {
              case 'Y': System.out.println("--continue--");
                        break;

              case 'N': System.out.println("--bye--"); 
                        Runtime.getRuntime().halt(0);
                        break;   

                        default:System.out.println("Invalid selection!"); 
                  }

                  }while(choice2!='Y');
                        System.out.println("Enter your Payment: ");
                        payment = pay.nextInt();
                        change = payment - total;
                        System.out.println(" Your Payment is " + payment + " and your change is " + change);
                        System.out.println("[---Thank You for Ordering---]");

              }}

1 个答案:

答案 0 :(得分:1)

你可以这样做,

首先初始化4个变量以跟踪订单,

case

然后在每个quantity语句中,在获得用户对A的输入后,从适当的变量中减去他订购的金额。

例如在A_left -= numb; // subtract the amount(quantity ordered) if (A_left < 0) { System.out.println("You can only choose 4 items max from A"); break; } 的情况下,

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

希望这有帮助!