0
私は既に異なるスイッチのケースで宣言したさまざまな変数を出力しようとしていますが、それらはすべてforループのswitchブロックにありますが、コンパイラは変数を使用できません。私に "シンボルを見つけることができません"というエラーが表示されます。スイッチケースの外の変数java
ここでコードは次のとおりです。
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私はこのプロジェクトのためのメソッド、厳密に条件文とループを使用することはできません。
もし誰も気にしないのであれば、私は "ペイントブラシ"の文字列入力をしようとしていますが、input.next()を使用するとスペースに問題があり、input.nexLine入力はプログラムを終了したので、next()に戻る必要がありました。
誰でも助けていただければ幸いです。ありがとうalot
ありがとうございました。今私の問題は、ケース1からケース2に同じ値を転送しています。 –
@ElizabethEkefreループが必要なようです。 'while'ループの外側に変数を作成すると、各コマンドで利用できるようになります。 – Kayaman