2017-02-11 63 views
-4

基本上,我嘗試使用交換機,但是當我運行它時,它不會執行我製作的System.out.println。它編譯沒有問題。我不擅長編程,所以請解釋你的答案。交換機沒有按指令執行

import java.util.*; 
public class MrCoffee { 
    public static void main(String[] args) { 
     String a; 
     char d = 'y'; 
     Scanner mock = new Scanner(System.in); 
     System.out.println("Hi, I am Mr.Coffee what is your name?"); 
     a = mock.nextLine(); 
     System.out.println("Thank you " + a + "."); 
     System.out.println(a + ", do you have a validate rewards card currrently with you?"); 
     String b = ("y"), c = ("n"); 
     System.out.println("If yes enter the letter y. If no, please enter the letter n."); 
     d = mock.next().charAt(0); 
     if (d == 'y') { 
      Scanner low = new Scanner(System.in); 
      System.out.println("Ok, " + a + " please enter the amount of coffee you have bought today."); 
      int coffeeam, none, one, two, three, value = 1, value2 = 4; 
      coffeeam = low.nextInt(); 
      double total = coffeeam * 2.85; 
      String start = ("Thank you Alexander, you will recieve"), rat = ("The total due amount is"); 
      none = coffeeam/20; 
      switch (none) { 
      case 1: 
       System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee."); 
       break; 
      case 2: 
       System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee."); 
       break; 
      case 3: 
       System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee."); 
       break; 
      case 4: 
       System.out.println("Unfortunately " + a + ", this is not enough to recieve any free coffee."); 
       break; 
      case 5: 
       System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 6: 
       System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 7: 
       System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 8: 
       System.out.println(start + " 1 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 9: 
       System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 10: 
       System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 11: 
       System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 12: 
       System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 13: 
       System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 14: 
       System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 15: 
       System.out.println(start + " 2 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 16: 
       System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 17: 
       System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 18: 
       System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 19: 
       System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      case 20: 
       System.out.println(start + " 3 free coffee. " + rat + " " + total + " dollars."); 
       break; 
      } 
     } else if (d == 'n') { 
      System.out.println("Sorry " + a + ", since you do not have a rewards card. I will not be able to decide on the number of free coffee you can get."); 
      System.out.println("I would reccomend that you go to speak with one of our employees to register for a card."); 
     } 
    } 
} 
+0

給出測試用例。 – Pbd

+0

我縮減了代碼,使其更具可讀性(大多數IDE都可以爲你做,所以你應該在將來自己做)。但讓我們從頭開始。你的代碼如何工作?你期望的是什麼軟件?爲什麼? – Pshemo

+1

從你的代碼中,一個人直到他購買了100個coff時纔會得到一個免費的咖啡,我們需要你解釋你正在努力實現的目標。通過這種方式你可以聲明一堆你永遠不會使用的變量。 – TheBakker

回答

1

確定這樣的問題是,由於在劃分的整數由一個整數:

none = coffeeam/20; 

然後,如果遊戲者使在低於20的任何值,例如coffeeam = 19,那麼沒有將爲0,因爲它將它舍入爲0.因此,您切換不運行,因爲大部分時間的值都是0。我不完全知道您爲什麼要將它除以20,但如果您可以詳細說明爲什麼你除以20,我可以提供一個替代解決方案,以避免整數四捨五入的問題。

此外,我還想補充說,你的開關似乎真的效率低下。如果指定一個字符串變量並僅運行一個System.out.println方法,則更好:如

String message; 
switch (variable) { 
case 1: 
    message = "Cat"; 
    break; 
case 2: 
    message = "Dog"; 
    break; 
default: 
    message = "None"; 
    break; 
} 
System.out.println(message);