Java Can';我不能让它工作
我知道开关中的任何变量都必须在case语句的范围之外声明,但我尝试将所有计算抛出开关范围之外,但我仍然无法从该脚本中获得所需的“总计”结果。有人能帮我吗?我只想让这个脚本最后的block-commented语句起作用Java Can';我不能让它工作,java,Java,我知道开关中的任何变量都必须在case语句的范围之外声明,但我尝试将所有计算抛出开关范围之外,但我仍然无法从该脚本中获得所需的“总计”结果。有人能帮我吗?我只想让这个脚本最后的block-commented语句起作用 import java.util.Scanner; public class script { public static void main(String[] args) { //initialization Scanner key = new Sc
import java.util.Scanner;
public class script
{
public static void main(String[] args)
{
//initialization
Scanner key = new Scanner(System.in);
final double COST_WINE = 13.99; // What is the size of the bottle of wine? ***********************************
final double COST_BEER_6 = 11.99, COST_BEER_12 = 19.99, COST_BEER_24 = 34.99;
final double spirits_750 = 25.99, spirits_1000 = 32.99;
final double gst = 0.05; //confirm if this number is right***************************************************
final double deposit750 = 0.10, deposit1000 = 0.25;
double wineSubTotal, wineTotal , total_cost;
double beer_6_SubTotal, beer_6_Total;
double beer_12_SubTotal, beer_12_Total;
double beer_24_SubTotal, beer_24_Total;
double spirits_750_SubTotal, spirits_750_Total;
double spirits_1000_SubTotal, spirits_1000_Total;
int wineQuantity, beer_6_Quantity, beer_12_Quantity;
int beer_24_Quantity, spirits_750_Quantity, spirits_1000_Quantity;
int beer_choice, spirits_choice;
char choice;
String input, again;
//loop for fun
for(int i = 0 ; i < 40; i++)
{
System.out.print("-");
}
System.out.printf("\n*** Welcome to Yoshi's Liquor Mart ***\n");
System.out.printf("Today's date is \n"); // Reserved for date***********************************************
//another loop for fun
for(int i = 0 ; i < 40; i++)
{
System.out.print("-");
}
//first actual loop, not for fun anymore
do
{
System.out.printf("\nWine is $13.99" + "\nBeer 6 pack is $11.99\n" + "Beer 12 pack is $19.99\n" +
"Beer 24 pack is $34.99\n" + "Spirits 750 mL is $25.99\n" + "Spirits 1000 mL is $32.99\n" +
"What is the item being purchased?\n" + "W for Wine, B for Beer and S for Spirits, or X to quit: ");
input = key.nextLine();
choice = input.charAt(0);
switch(choice)
{
case('w'):
case('W'):
System.out.printf("\nHow many bottles of wine is being purchased? ");
wineQuantity = key.nextInt();
wineSubTotal = wineQuantity * COST_WINE;
wineTotal = (wineSubTotal * gst) + wineSubTotal + (deposit750 * wineQuantity);
System.out.printf("\nThe cost of " + wineQuantity +
" bottles of wine including GST and deposit is %.2f\n", wineTotal);
break;
case('b'):
case('B'):
System.out.printf("What is size of the beer pack?\n" + "6, 12 or 24?");
beer_choice = key.nextInt();
if(beer_choice == 6)
{
System.out.printf("\nHow many 6 pack of beer is being purchased?");
beer_6_Quantity = key.nextInt();
beer_6_SubTotal = beer_6_Quantity * COST_BEER_6;
beer_6_Total = (beer_6_SubTotal * gst) + beer_6_SubTotal;
System.out.printf("\nThe cost of %d cases of 6 pack of beer" +
"including GST and deposit is %.2f\n", beer_6_Quantity, beer_6_Total);
}
else if(beer_choice == 12)
{
System.out.printf("How many 12 pack of beer is being purchased?");
beer_12_Quantity = key.nextInt();
beer_12_SubTotal = beer_12_Quantity * COST_BEER_12;
beer_12_Total = (beer_12_SubTotal * gst) + beer_12_SubTotal;
System.out.printf("\nThe cost of %d cases of 12 pack of beer" +
"including GST and deposit is %.2f\n", beer_12_Quantity, beer_12_Total);
}
else if(beer_choice == 24)
{
System.out.printf("How many 24 pack of beer is being purchased?");
beer_24_Quantity = key.nextInt();
beer_24_SubTotal = beer_24_Quantity * COST_BEER_24;
beer_24_Total = (beer_24_SubTotal * gst) + beer_24_SubTotal;
System.out.printf("\nThe cost of %d cases of 24 pack of beer" +
"including GST and deposit is %.2f\n", beer_24_Quantity, beer_24_Total);
}
else
{
System.out.println("Invalid number");
}
break;
case('s'):
case('S'):
System.out.printf("What is size of Spirits?\n" + "750ml or 1000ml?");
spirits_choice = key.nextInt();
if(spirits_choice == 750)
{
System.out.printf("How many bottles are being purchased?");
spirits_750_Quantity = key.nextInt();
spirits_750_SubTotal = spirits_750_Quantity * spirits_750;
spirits_750_Total = (spirits_750_SubTotal * gst) + spirits_750_SubTotal +
(deposit750 * spirits_750_Quantity);
System.out.printf("\nThe cost of %d bottle(s) of Spirits 750ml " +
"including GST and deposit is %.2f\n", spirits_750_Quantity, spirits_750_Total);
}
else if(spirits_choice == 1000)
{
System.out.printf("How many bottles are being purchased?");
spirits_1000_Quantity = key.nextInt();
spirits_1000_SubTotal = spirits_1000_Quantity * spirits_1000;
spirits_1000_Total = (spirits_1000_SubTotal * gst) + spirits_1000_SubTotal +
(deposit1000 * spirits_1000_Quantity);
System.out.printf("\nThe cost of %d bottle(s) of Spirits 1000ml " +
"including GST and deposit is %.2f\n", spirits_1000_Quantity, spirits_1000_Total);
}
else
{
System.out.println("Invalid number");
}
break;
case('x'):
case('X'):
break;
default:
System.out.printf("Invalid choice");
break;
}
System.out.println("Is this customer's order complete? ");
again = key.nextLine();
}
while(again.equals("n"));
//Total cost
/*total_cost = wineTotal + beer_6_Total + beer_12_Total + beer_24_Total +
spirits_750_Total + spirits_1000_Total;
System.out.printf("The total cost for this customer is %f", total_cost);*/
key.close(); //There was a warning message(not error), so I found out on StackOverFlow that this would close the Scanner
}
}
import java.util.Scanner;
公共类脚本
{
公共静态void main(字符串[]args)
{
//初始化
扫描仪键=新扫描仪(System.in);
最终双倍成本_WINE=13.99;//这瓶酒的大小是多少***********************************
最终双倍成本啤酒6=11.99,成本啤酒12=19.99,成本啤酒24=34.99;
最终双份烈酒750=25.99,烈酒1000=32.99;
最终双gst=0.05;//确认此数字是否正确***************************************************
最终双倍存款750=0.10,存款1000=0.25;
双倍葡萄酒小计、葡萄酒总计、总成本;
双倍啤酒6小计,啤酒6小计;
双倍啤酒小计,啤酒总数;
双倍啤酒24小时小计,啤酒24小时总计;
双份烈酒小计,烈酒总数;
双份烈酒小计,烈酒总数;
国际葡萄酒数量、啤酒6数量、啤酒12数量;
国际啤酒24杯、烈酒750杯、烈酒1000杯;
int啤酒精选、烈酒精选;
字符选择;
再次输入字符串;
//环游
对于(int i=0;i<40;i++)
{
系统输出打印(“-”);
}
System.out.printf(“\n***欢迎来到Yoshi's酒市场***\n”);
System.out.printf(“今天的日期是\n”);//保留日期***********************************************
//另一个有趣的循环
对于(int i=0;i<40;i++)
{
系统输出打印(“-”);
}
//第一个实际循环,不再是为了好玩
做
{
System.out.printf(“\n行是$13.99”+”\n第6包是$11.99\n“+”啤酒12包是$19.99\n”+
“啤酒24包$34.99\n”+“烈酒750毫升$25.99\n”+“烈酒1000毫升$32.99\n”+
“正在购买的项目是什么?\n”+“W表示葡萄酒,B表示啤酒,S表示烈酒,或X表示退出:”;
输入=key.nextLine();
选择=输入.charAt(0);
开关(选择)
{
案例(“w”):
案例(“W”):
System.out.printf(“\n购买了多少瓶葡萄酒?”);
wineQuantity=key.nextInt();
葡萄酒小计=葡萄酒数量*葡萄酒成本;
葡萄酒总计=(葡萄酒小计*消费税)+葡萄酒小计+(存款750*葡萄酒数量);
System.out.printf(“\n成本”+葡萄酒数量+
“包括GST和押金在内的葡萄酒瓶数为%.2f\n”,葡萄酒总数);
打破
案例(“b”):
案例(“B”):
System.out.printf(“啤酒包装的尺寸是多少?\n”+“6、12或24?”);
beer_choice=key.nextInt();
如果(啤酒选择==6)
{
System.out.printf(“\n购买了多少6包啤酒?”);
啤酒数量=key.nextInt();
啤酒小计=啤酒数量*成本啤酒6;
啤酒总计=(啤酒小计*消费税)+啤酒小计;
System.out.printf(“\n 6包啤酒%d箱的成本”+
“包括GST和押金为%.2f\n”,啤酒数量,啤酒总量);
}
否则如果(啤酒选择==12)
{
System.out.printf(“购买了多少12包啤酒?”);
啤酒数量=key.nextInt();
啤酒小计=啤酒数量*成本啤酒12;
啤酒总计=(啤酒小计*消费税)+啤酒小计;
System.out.printf(“\n 12包啤酒%d箱的成本”+
“包括GST和押金为%.2f\n”,啤酒数量,啤酒总量);
}
否则如果(啤酒选择==24)
{
System.out.printf(“购买了多少24包啤酒?”);
啤酒24_数量=key.nextInt();
啤酒小计=啤酒数量*成本啤酒数量;
啤酒24小时总计=(啤酒24小时小计*消费税)+啤酒24小时小计;
System.out.printf(“\n%d箱24包啤酒的成本”+
“包括GST和押金为%.2f\n”,啤酒数量,啤酒总量);
}
其他的
{
System.out.println(“无效编号”);
}
打破
案例('s'):
案例('S'):
System.out.printf(“烈酒的大小是多少?\n”+“750ml还是1000ml?”);
spirits_choice=key.nextInt();
如果(选择==750)
{
System.out.printf(“购买了多少瓶?”);
烈酒数量=key.nextInt();
烈酒小计=烈酒数量*烈酒750;
烈酒总计=(烈酒小计*gst)+烈酒小计+
(存款750*酒精750\单位数量);
System.out.printf(“\n%d瓶750ml酒精的成本”+
“包括
case ('w'):
case ('W'): {
System.out.printf("\nHow many bottles of wine is being purchased? ");
int wineQuantity = key.nextInt();
double wineSubTotal = wineQuantity * COST_WINE;
wineTotal = (wineSubTotal * gst) + wineSubTotal + (deposit750 * wineQuantity);
System.out.printf(
"\nThe cost of " + wineQuantity + " bottles of wine including GST and deposit is %.2f\n",
wineTotal);
}
break;