切换案例java

时间:2016-11-27 17:31:18

标签: java string variables console switch-statement

我试图打印出我已经在不同开关情况下声明的不同变量,它们都在for循环中的一个开关块中,但编译器不允许我使用变量。给我一个"找不到符号"错误。

以下是代码:

public static void main(String[] args) {
    double hammer = 3.25, nails = 5.25, paint = 4.75, paintBrush = 2.25, balance = 50.00;
    Scanner input = new Scanner(System.in);
    System.out.println("Local Hardware Point of Sales");
    System.out.println("\t::MENU::");
    System.out.println("1. Purchase Items\n2. Display current purchases\n3. Display account balance\n4. Complete transactions and Exit");
    System.out.print("Enter choice: ");
    int choice = input.nextInt();
    switch(choice){

        case 1: 
                     for(int i = 0; i <= 2; i++){      
            System.out.println("\n\tPurchase Items");
            System.out.println("What items would you like to purchase?");
            System.out.println("\tItems \t\tPrices");
            System.out.println("\tHammer\t\t-$3.25\n\tNails\t\t-$3.25\n\tPaint\t\t-$3.25\n\tPaint Brush\t-$3.25");
            String item = input.next(); 
    switch (item) {
        case "Hammer":
        case "hammer":
            System.out.println("How many Hammers would you like to purchase?");
            int hItem = input.nextInt();
            System.out.println("Starting Balance: $" + balance);
            double hPrice = hItem*hammer;
            System.out.println("Cost for " + hItem + " Hammers: $" + hPrice);
            double hBalance = balance - hPrice;
            System.out.println("Final Balance: $" + hBalance);
            if(hBalance > 50.00){
                System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
                System.out.println("You will be returned to the main menu...");  
            }
            else{
                System.out.println("\nPurchase the "+ hItem+ " Hammers (Y/N)");
                String ch = input.next();
                if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
                    System.out.println("You purchased "+hItem+"Hammers for "+ hPrice);   
                }

            }
            break;

        case "Nails":
        case "nails":
            System.out.println("How many Nails would you like to purchase?");
            int nItem = input.nextInt();
            System.out.println("Starting Balance: $" + balance);
            double nPrice = nItem*nails;
            System.out.println("Cost for " + nItem + " Nails: $" + nPrice);
            double nBalance = balance - nPrice;
            System.out.println("Final Balance: $" + nBalance);
            if(nBalance > 50.00){
                System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
                System.out.println("You will be returned to the main menu...");  
            }
            else{
                System.out.println("\nPurchase the "+ nItem+ " Nails (Y/N)");
                String ch = input.next();
                if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
                System.out.println("You purchased "+nItem+" Nails for "+ nPrice);   
                }  
            }
            break;

        case "Paint":
        case "paint":
            System.out.println("How many Paints would you like to purchase?");
            int pItem = input.nextInt();
            System.out.println("Starting Balance: $" + balance);
            double pPrice = pItem*nails;
            System.out.println("Cost for " + pItem + " Paints: $" + pPrice);
            double pBalance = balance - pPrice;
            System.out.println("Final Balance: $" + pBalance);
            if(pBalance > 50.00){
                System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
                System.out.println("You will be returned to the main menu...");  
            }
            else{
                System.out.println("\nPurchase the "+ pItem+ " Paints (Y/N)");
                String ch = input.next();
                if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
                System.out.println("You purchased "+pItem+" Paints for "+ pPrice);   
                }  
            }
            break;

        case "Paint Brush":
        case "paint brush":
            System.out.println("How many Paint Brushes would you like to purchase?");
            int pbItem = input.nextInt();
            System.out.println("Starting Balance: $" + balance);
            double pbPrice = pbItem*nails;
            System.out.println("Cost for " + pbItem + " Paint Brushes: $" + pbPrice);
            double pbBalance = balance - pbPrice;
            System.out.println("Final Balance: $" + pbBalance);
            if(pbBalance > 50.00){
                System.out.println("WARNING: YOU HAVE INSUFFICIENT FUNDS FOR THIS PURCHASE. YOU CANNOT PURCHASE 30 NAILS.");
                System.out.println("You will be returned to the main menu...");  
            }
            else{
                System.out.println("\nPurchase the "+ pbItem+ " Paint Brushes (Y/N)");
                String ch = input.next();
                if(ch.equals("Yes") || (ch.equals("Y")) || (ch.equals("y")) ){
                 System.out.println("You purchased "+pbItem+" Paint Brushes for "+ pbPrice);   
                }  
            }
            break;

        default:
            break;
    }
    System.out.println("\nMake another purchase? (Y/N)");
      String ans = input.next();
      if(ans.equals("n"))
          System.out.println("\tCurrent Receipt");
      System.out.println("Items \t\tQuantity \t\tPrice Per \t\tTotal Price");
      System.out.println("Hammer \t\t"+**hItem**+" \t\t\t"+hammer+" \t\t\t"+**hPrice**);
      System.out.println("Nails \t\t"+**nItem**+" \t\t\t"+nails+" \t\t\t"+**nPrice**);
      System.out.println("Paint \t\t"+**pItem**+" \t\t\t"+paint+" \t\t\t"+**pPrice**);
      System.out.println("Paint Brush \t\t"+**pbItem**+" \t\t\t"+paintBrush+" \t\t\t"+**pbPrice**);
     }
    }
}

}

我加粗了给出错误的变量。 P.s我不允许使用这个项目的方法,严格的条件语句和循环。

此外,如果有人不介意,我会尝试为#34; Paint Brush&#34;但是使用input.next()时空间有问题,当我使用input.nexLine()时,它完全忽略了输入并结束了程序,所以不得不恢复到next()。

如果有人可以提供帮助,请多多感激。非常感谢

1 个答案:

答案 0 :(得分:1)

声明在switch-case之外给出错误的变量。目前,他们的范围仅限于E/AndroidRuntime: FATAL EXCEPTION: Thread-4 Process: com.example.lucadigiammarino.biogame, PID: 3569 java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.<init>(Handler.java:200) at android.os.Handler.<init>(Handler.java:114) at android.os.CountDownTimer$1.<init>(CountDownTimer.java:114) at android.os.CountDownTimer.<init>(CountDownTimer.java:114) at com.example.lucadigiammarino.biogame.GameCountDownTimer.<init>(GameCountDownTimer.java:0) at com.example.lucadigiammarino.biogame.GameView.startTimer(GameView.java:135) at com.example.lucadigiammarino.biogame.GameView.run(GameView.java:306) at java.lang.Thread.run(Thread.java:761) 部分的内部,这意味着对于其余代码([m,n] = size(A)之外),它们不存在。< / p>