おはようございます/午後スイッチの正しい使い方は?メニューオプション注文サンドイッチ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
コードをテストしましたか?なぜなら、if文/コードブロックはあなたが考えるように動作しないからです。 if文のすべて( '{'から '}'まで)は、条件が真である場合にのみ発生します。したがって、他のif文は実行されません。なぜ彼らを巣に入れたのですか?それらを入れ子にしないでください。お互いの下に置くだけです(末尾にない次のif文の前に '}'を入れてください)。 – Dukeling
また、これは 'switch'の適切な使い方ですが、チェックしている2つの条件だけでなく、作業コードが「正しい使用」であるかどうかを尋ねる場合は、代わりにifステートメントを使用することもできますそれはしばしば意見になるだろうから、そうしようとするものとは拮抗しているからです。 – Dukeling
最初の 'switch'の後に' break; 'が表示されないので、おそらくこれでエラーが発生します。ネストされた' if'の代わりにそれらを変更しようとします。 'switch'を使用して現在の' switch' 'if'を使って。これは基本的に 'if'がいくつかのオプションを使用しているのに対し、' switch'は多くの時によく使われます。 – 3vts