2017-04-07 4 views
0

おはようございます/午後スイッチの正しい使い方は?メニューオプション注文サンドイッチORサラダ

以下は私のJavaクラス用に書いたコードです(学校の宿題のように)。私は誰かが私のために宿題をやろうとしているのではない。

私の質問は、スイッチ/ケースをどのように設定することができますか? 入力1または2の入力を求めるプロンプトが表示されます。回答1サンドイッチを取得しています。回答2はサラダを取得しています。

このプログラムの説明書は、顧客から始まるサンドイッチまたはサラダを注文します。その後、追加料金でトッピングを追加して、余分なトッピングを拒否することもできます。注文と合計を印刷します。教授はIFステートメントとスイッチを使用するように指定しました。しかし、スタック上で見つかった例のIF文の代わりにスイッチが使われているように見えるので、私は何かが足りないと感じています。ご協力いただきありがとうございます。

import java.util.Scanner; 

//loaded scanner to take user input 
public class Restaurant 


//dude change this, do 3 outputs asking for input 1 input 2 input 3 make those if statements after 
{ 
/* Author: 
    Date: 
    Program: create a class that will offer a sandwich for $7.00 with optional three 
    toppings lettuce, tomato, cheese $1.00 each 
    or a salad with optional two toppings tomatos, cheese $0.50 each. 
*/ 
    public static void main(String[] args) 

    { 

    // Declarations sandwich,salad,Sandwich-cheese,Sandwich-tomato,Sandwich-lettuce, salad-cheese, salad-tomato. 
     double sandwich = 7.00; 
     double salad = 7.00; 
     double sandChe = 1.00; 
     double sandTom = 1.00; 
     double sandLet = 1.00; 
     double salChe = .50; 
     double salTom = .50; 
     int userInput; 
     int userInput1; 
     int userInput2; 
     int userInput3; 
     double sandTotal; 
     double saladTotal; 

     Scanner scanner = new Scanner(System.in); 

     System.out.println("Enter 1 for a Sandwich or 2 for a Salad"); 
     int userInput = scanner.nextLine(); 

     switch(userInput) 
     { 
     case 1: // a sandwich was ordered 




      System.out.println("Enter 1 for additional topping of lettuce or press 2"); 
      int userInput1 = scanner.nextLine(); 


      System.out.println("Enter 1 for additional topping of cheese or press 2"); 
      int userInput2 = scanner.nextLine(); 


      System.out.println("Enter 1 for additional topping of tomato or press 2"); 
      int userInput3 = scanner.nextLine(); 



      if (userInput1 == 1 && userInput2 == 1 && userInput3 == 1) 
      { 
       saladTotal = (sandwich + sandLet + sandChe + sandTom); 
       System.out.println("Your bill comes to a total of: " + sandTotal + " Thank you, Have a great day!"); 



       if (userInput1 == 1 && userInput2 == 2 && userInput3 == 2) 
       { 
        sandTotal = (sandwich + sandLet); 
        System.out.println("Your bill for a salad with additional tomato toppings comes too: " + sandTotal + " Thank you, Have a great day!"); 


        if (userInput1 == 1 && userInput2 == 1 && userInput3 == 2) 
        { 
        sandTotal = (sandwich + sandLet + sandChe); 
        System.out.println("Your bill for a salad with no additional toppings comes too: " + salad + " Thank you, Have a great day!"); 


        if (userInput1 == 1 && userInput2 == 2 && userInput3 == 1) 
        { 
         sandTotal = (sandwich + sandLet + sandTom); 
         System.out.println("Your bill for a sandwich `enter code here`lettuce and tomato comes too: " + sandTotal + " Thank you, Have a great day!");  


         if (userInput1 == 2 && userInput2 == 1 && userInput == 1) 
         { 
          sandTotal = (sandwich + sandChe + sandTom); 
          System.out.println("Your bill for a sandwich with cheese and tomato comes too: " + sandTotal + " Thank you, Have a great day!"); 


          if (userInput1 == 2 && userInput2 == 2 && userInput3 == 2) 
          { 
           System.out.println("Your bill for a sandwich comes too: " + sandwich + " Thank you, Have a great day!"); 
          }// end if 1 
         }//end if 2 
        }//end if 3 
        }//end if 4 
       }//end if 5 
      }//end if 6 

     } 
    } 
}    


     // switch case below 


     case 2: // a salad was ordered 




      { 
       System.out.println("Press 1 for additional topping of cheese or press 2"); 
       int userInput1 = scanner.nextLine(); 



       System.out.println("Press 1 for additional topping of tomato or press 2"); 
       int userInput2 = scanner.nextLine(); 


       if (userInput1 == 1 && userInput2 == 2) 
       { 
        saladTotal = (salad + salChe); 
        System.out.println("Your bill comes to a total of: " + saladTotal + " Thank you, Have a great day!"); 



        if (userInput1 == 2 && userInput2 == 1) 
        { 
        saladTotal = (salad + salTom); 
        System.out.println("Your bill for a salad with additional tomato toppings comes too: " + saladTotal + " Thank you, Have a great day!"); 


        if (userInput1 == 2 && userInput2 == 2) 
        { 
         System.out.println("Your bill for a salad with no additional toppings comes too: " + salad + " Thank you, Have a great day!"); 


         if (userInput1 == 1 && userInput2 == 1) 

         { 
          saladTotal = (salad + salChe + salTom); 
          System.out.println("Your bill for a salad with Tomato and Cheese toppings comes too: " + saladTotal + " Thank you, Have a great day!"); 

         }//end if 1 
        }//end if 2 
        }//end if 3 
       }//end if 4 


      } 



}// end of class 
+0

コードをテストしましたか?なぜなら、if文/コードブロックはあなたが考えるように動作しないからです。 if文のすべて( '{'から '}'まで)は、条件が真である場合にのみ発生します。したがって、他のif文は実行されません。なぜ彼らを巣に入れたのですか?それらを入れ子にしないでください。お互いの下に置くだけです(末尾にない次のif文の前に '}'を入れてください)。 – Dukeling

+0

また、これは 'switch'の適切な使い方ですが、チェックしている2つの条件だけでなく、作業コードが「正しい使用」であるかどうかを尋ねる場合は、代わりにifステートメントを使用することもできますそれはしばしば意見になるだろうから、そうしようとするものとは拮抗しているからです。 – Dukeling

+0

最初の 'switch'の後に' break; 'が表示されないので、おそらくこれでエラーが発生します。ネストされた' if'の代わりにそれらを変更しようとします。 'switch'を使用して現在の' switch' 'if'を使って。これは基本的に 'if'がいくつかのオプションを使用しているのに対し、' switch'は多くの時によく使われます。 – 3vts

答えて

0

通常、switch-caseステートメントとif/elseブロックは互換性があります。スイッチケースは、break(以下の例)の使用のために少し異なります。ほとんどの場合、if文が使用されます。私は通常、列挙に基づいてケースを処理するためにスイッチケースを使用します。

FoodType chosenType = FoodType.SANDWITCH; // can be user input 

switch (chosenType) { 
case SANDWITCH: 
    System.out.println("you want cheese?"); 
case SALAD: 
    System.out.println("Sauce?"); 
    break; 
case FRUIT: 
    System.out.println("which one?"); 
    break; 
default: 
    throw new IllegalStateException(); 
} 

:私はおそらくこのような何かをするだろう、私は彼が選択した食品に基づいてユーザの質問をしたいのであれば

public enum FoodType { 
    SANDWITCH, 
    SALAD, 
    FRUIT 
} 

は、私たちが列挙FoodTypeを考えてみましょうここをクリックしてください:

  • サンドウィッチを選択すると、「あなたはチーズが欲しいですか? 「ソース」? breakステートメントがないので、印刷されます。
  • FRUITが選択されている場合、「which one?」だけが選択されます。印刷されます。
  • defaultは、ユーザーがSANDWITCHまたはSALADまたはFRUIT以外のものを選択した場合に発生します。 if文

    if (FoodType.SALAD.equals(chosenType)) {...} 
    

    いくつかの例があるとき、私は評価者の簡単な使用します。

もちろん、同様if文でこれを書くことができます。また、「ケース」内のコードは、過度に長く複雑なものであってはなりません。私はあなたがいつどのステートメントを使うかを知りたいと思っていると思います。確かに非常に良いブログ記事やstackoverflowの回答があります。

// Sidenote:default:はこの例では到達可能なコードではないことを認識しています。

+0

すばらしい例と説明!私は、スイッチとif文を使用するよう教授の周りに混乱が起こっていると信じています。スイッチを使用すると、ケース "s"は非常に長く複雑になります(少なくとも私のスキルセットでは)。もし私がIf文を使用していれば、これは望みの結果を出すでしょうが、単純な答えサンドイッチまたはサラダを持つ最初のオプションに使用しない限り、スイッチの使用は省略します。あなたのご意見ありがとうございます! – Elements

関連する問題