有没有办法可以制作案例' x'在交换机内部终止程序而不继续循环并打印"该客户的订单是否完整? (Y / N)"在终止之前。
import java.util.Scanner;
import java.util.Date;
public class YoshisLiqourMart {
public static void main (String [] args){
//Variables
//CONSTANTS
final double GSTTAX = 0.05;
final double SMALLDEPOSIT = 0.10;
final double LARGEDEPOSIT = 0.25;
//PRICE
double wine_Price = 11.99;
double beer6_Price = 15.99;
double beer12_Price = 24.99;
double beer24_Price = 39.99;
double spirits750_Price = 25.99;
double spirits1000_Price = 35.99;
//SELECTION
char selection = 0 ;
char completeOrder = 'Y';
char quitProgram = 'X';
//WINE
int winePurchase;
double wineDeposit =0.10;
double wineTotal;
double wineGST;
//BEER
int beerSelection;
//Beer 6 Pack
int beer6Purchase;
double beer6Deposit = 0.60;
double beer6Total;
double beer6GST;
//Beer 12 Pack
int beer12Purchase;
double beer12Deposit =1.20 ;
double beer12Total;
double beer12GST;
//Beer
int beer24Purchase;
double beer24Deposit = 2.40;
double beer24Total;
double beer24GST;
//SPIRITS
int spiritsSelection;
//Spirits 750 mL
int spirits750Purchase;
double spirits750Deposit = .10;
double spirits750Total;
double spirits750GST;
//Spirits 1000 mL
int spirits1000Purchase;
double spirits1000Deposit = .25;
double spirits1000Total;
double spirits1000GST;
System.out.println("--------------------------------------");
System.out.println("*** Welcome to Yoshi's Liquor Mart *** ");
System.out.println("Today's date is:" );
System.out.println("--------------------------------------");
//OPTIONS
String options ="\n+++++++++++++++++++++++++++++++++++"+
"\n| Item | Quantity | Price |"+
"\n+++++++++++++++++++++++++++++++++++"+
"\n| Wine | bottle | $11.99 |"+
"\n| Beer | 6 pack | $15.99 |"+
"\n| Beer | 12 pack | $24.99 |"+
"\n| Beer | 24 pack | $39.99 |"+
"\n| Spirits | 750 mL | $25.99 |"+
"\n| Spirits | 1000 mL | $35.99 |"+
"\n+++++++++++++++++++++++++++++++++++";
//Prompts
Scanner yoshisliquormart = new Scanner (System.in);
do{
System.out.print(options);
System.out.print("\nWhat is the item being purchased?\nW for Wine, B for Beer and S for Spirits, or X to quit:");
selection = yoshisliquormart.next().toUpperCase().charAt(0);
switch (selection){
case'X':
System.out.print("Thank you.");
quitProgram++;
break;
//Wine
case'W':
System.out.print("How many bottles of wine is being purchase?");
winePurchase = yoshisliquormart.nextInt();
//CALCULATION
wineTotal =(winePurchase * (wine_Price+wineDeposit));
wineGST = (wineTotal *GSTTAX);
System.out.printf("The cost of " +winePurchase + " x wine (including GST and deposit) is :%.2f",(wineTotal+wineGST));
break;
//Beer
case 'B':
System.out.print("What is the size of the beer pack 6,12, or 24?");
beerSelection =yoshisliquormart.nextInt();
if (beerSelection == 6){
System.out.print("How many 6 pack of beer is being purchased?");
beer6Purchase = yoshisliquormart.nextInt();
//CALCULATION
beer6Total = (beer6Purchase * (beer6_Price+beer6Deposit));
beer6GST = (beer6Total *GSTTAX);
System.out.printf("The cost of "+ beer6Purchase +" x 6 pack of beer (including GST & deposit)is :%.2f",(beer6Total+beer6GST));
}
if (beerSelection ==12){
System.out.print("How many 12 pack of beer is being purchased?");
beer12Purchase= yoshisliquormart.nextInt();
beer12Total = (beer12Purchase * (beer12_Price+beer12Deposit));
beer12GST = (beer12Total *GSTTAX);
System.out.printf("The cost of "+ beer12Purchase +" x 12 pack of beer (including GST & deposit)is :%.2f",(beer12Total+beer12GST));
}
if (beerSelection == 24){
System.out.print("How many 24 pack of beer is being purchased?");
beer24Purchase = yoshisliquormart.nextInt();
beer24Total = (beer24Purchase * (beer24_Price+beer24Deposit));
beer24GST = (beer24Total *GSTTAX);
System.out.printf("The cost of "+ beer24Purchase +" x 24 pack of beer (including GST & deposit)is :%.2f",(beer24Total+beer24GST));
}
break;
//Spirits
case 'S':
System.out.print("What size bottle of spirits (in mL) 750 or 1000?");
spiritsSelection = yoshisliquormart.nextInt();
if (spiritsSelection ==750){
System.out.print("How many bottles of 750 mL spirits is being purchased?");
spirits750Purchase = yoshisliquormart.nextInt();
//CALCULATION
spirits750Total = (spirits750Purchase * (spirits750_Price+spirits750Deposit));
spirits750GST = (spirits750Total *GSTTAX);
System.out.printf("The cost of "+spirits750Purchase + " x 1000 mL spirits (including GST & deposit):%.2f",(spirits750Total+spirits750GST));
}
if (spiritsSelection ==1000){
System.out.print("How many bottles of 1000 mL spirits is being purchased?");
spirits1000Purchase = yoshisliquormart.nextInt();
//Calculation
spirits1000Total = (spirits1000Purchase * (spirits1000_Price+spirits1000Deposit));
spirits1000GST = (spirits1000Total *GSTTAX);
System.out.printf("The cost of "+spirits1000Purchase + " x 1000 mL spirits (including GST & deposit):%.2f",(spirits1000Total+spirits1000GST));
}
break;
}
System.out.print("\nIs this customer's order complete? (Y/N)");
completeOrder = yoshisliquormart.next().toUpperCase().charAt(0);
if (completeOrder == 'Y'){
System.out.printf("The total for this customer is:");
}
}while (completeOrder == 'N');
}
}
答案 0 :(得分:0)
您可以在案例中设置一个布尔值,然后在切换案例后检查它。
实施例
boolean caseX = false;
...
case'X':
System.out.print("Thank you.");
quitProgram++;
caseX = true;
break;
...
if (caseX) break;
System.out.print("\nIs this customer's order complete? (Y/N)");
completeOrder = yoshisliquormart.next().toUpperCase().charAt(0);
但是你的代码似乎过于拥挤了。如果你将它清理干净并将其分解成单独的方法,清洁剂解决方案可能会显露出来。
答案 1 :(得分:0)
如果这个程序仅适合你自己和#34;良好的编程"你可以使用
这么重要System.exit(0);
终止进程并返回状态值0(通常状态值对小项目来说并不重要)
通常会使用布尔值和if-blocks,但由于你的代码已经难以阅读,我不会建议。
最好的方法是订购代码(创建一些小方法而不是这个超长方法),然后使用第二种方法(布尔值+ if-block)