2017-11-28 7 views
0

さて、私は明らかにこの質問に対して質問をしていません。アイデアは、フレーバーに関連付けられた数字(アイフルクリームが何であるかを示す)をユーザーに求めることです。任意の数値を入力するたびに、デフォルト値が吐き出されます。問題の1つ(少なくとも私はそれが...だと思います)はinput = keyboard.nextInt();です。スイッチの入力に関する問題

System.out.println(" "); 
    System.out.println(" What flavor of ice cream would you like? "); 
    System.out.println("(1) Vanilla (2) Peanut Butter (3) Chocolate (4)Chocolate Chip (5) Mint Chocolate Chip " + 
     " (6) Cookie Dough (7) Mint Mouse tracks (8) Coconut (9) Pinapple (10) Cotton Candy" + 
      "(11) Mouse Tracks (12) Oreo Cookies and Cream"); 
    input = keyboard.nextInt(); 
    flavorType = input.charAt(0); 

    switch(flavorType) 
    { 
     case 1: 
      order.setFlavor ("Vanilla"); 
      break; 
     case 2: 
      order.setFlavor("Peanut-Butter"); 
      break; 
     case 3: 
      order.setFlavor ("Chocolate"); 
      break; 
     case 4: 
      order.setFlavor ("Chocolate Chip"); 
      break; 
     case 5: 
      order.setFlavor ("Mint Chocolate Chip"); 
      break; 
     case 6: 
      order.setFlavor ("Choclate Chip Cookie Dough"); 
      break; 
     case 7: 
      order.setFlavor ("Mint Mouse Tracks"); 
      break; 
     case 8: 
      order.setFlavor ("Coconut"); 
      break; 
     case 9: 
      order.setFlavor ("Pinapple"); 
      break; 
     case 10: 
      order.setFlavor ("Cotton Candy"); 
      break; 
     case 11: 
      order.setFlavor ("Mouse Tracks"); 
      break; 
     case 12: 
      order.setFlavor ("Oreo Cookies and Cream"); 
      break; 
     default: 
     System.out.println(" "); 
     System.out.println(" Im sorry, that was not one of the choices, you will get two scoops " + 
      " of vanilla with whipped cream,hot fudge, rainbow sprinkles, and a cherry"); 
      order.setFlavor ("Vanilla"); 
    } 

さて、私はまた、このような

 String customerName; 
    double cost; 
    double toppingCost = 1.25; 
    int numberOfScoops; 
    int numberOfDeluxe = 0; 
    int numberOfToppings = 0; 
    //String flavor; 
    String toppingList; 
    final double TAX_RATE = .08625; 
    double tax; 
    char choice; 
    char flavorType; 
    String input; 
    String toppings = " Whipped cream, syrup, and sprinkles (Chocolate or rainbow)"; 
    int toppingChoice; 

として上に宣言された変数は、私はまた、ドライバプログラムに持ってきた...

public void setFlavor (String type) 
{ 
    flavor = type; 
} 

これは割り当ての一部のみです。それ以外の部分にはスクープ、トッピングなどが含まれます。しかし、この部分は問題であり、スイッチのステートメントを正しく取得できません。あなたと仮定すると

+1

それはコンストラクタではありません「コンストラクタを使用して、私が持っています」。 –

+1

"' input.charAt(0) '"これの値を印刷して、何が間違っているかを確認してください。また、11を入力しようとするとどうなるかを見てください。 –

+1

'nextInt'として' input'を読み込み、文字列メソッドである 'charAt'を試しています... – alfasin

答えて

0

が宣言:

Scanner keyboard = new Scanner(System.in); 

orderも同様に適切に定義されています。コメントに書いたとおり、inputintと表示されますが、input.charAtを呼び出すと、Stringと表示されます。

Mapを使用すると、オプションを表示するだけでなく、大文字と小文字を区別することなく、コードを簡略化することができます。

次のコードは、Java 9で動作します:

Map<Integer, String> dict = new TreeMap(Map.of(1, "Vanilla", 
                2, "Peanut-Butter", 
                3, "Chocolate", 
                4, "Chocolate Chip", 
                5, "Mint Chocolate Chip", 
                6, "Choclate Chip Cookie Dough", 
                7, "Mint Mouse Tracks", 
                8, "Coconut", 
                9, "Pinapple", 
                10, "Cotton Candy")); 

    // Map.of API allows up to 10 items - so we'll need to add the other two manually. 
    // That's also the reason that we're copying the Map.of by doing: 
    // new TreeMap(Map.of(...)) - since `Map.of` returns an immutable Map which we cannot modify 
    dict.put(11, "Mouse Tracks"); 
    dict.put(12, "Oreo Cookies and Cream"); 

    System.out.println("\n What flavor of ice cream would you like? "); 
    dict.forEach((k, v) -> System.out.println(String.format("(%d) %s", k, v))); 

    Scanner keyboard = new Scanner(System.in); 
    int input = keyboard.nextInt(); 

    if (null == dict.get(input)) { 
     System.out.println(" Im sorry, that was not one of the choices, you will get two scoops " + 
           " of vanilla with whipped cream,hot fudge, rainbow sprinkles, and a cherry"); 
     order.setFlavor ("Vanilla"); 
    } else { 
     order.setFlavor (dict.get(input)); 
    } 
+2

ええ、私はそれを使用することはできません... – Frostfox

+0

連続キーを持つ整数キーマップは、リストとしてより簡単に書き込まれます。 (また、マップ/リストDRYからオプションメッセージをビルドします)。 –

+0

@AndyTurner良い点は、TreeMapに切り替える必要がありました - しかし、それは間違いなくよりよく見えます - ありがとう! – alfasin

関連する問題