如何让客户继续购物,直到客户决定停止?我不知道为什么我的循环不起作用



您需要为销售硬件的公司编写Java程序通过互联网。您的程序将列出所有可用的硬件及其相应的价格供客户选择。您的客户需要输入所选项目和该项目的数量。您的程序应该计算客户需要付费。总价还必须包括3%的税。为了方便您,该公司只销售5种商品自由选择出售什么商品,价格是多少。程序将保持要求顾客继续购物直到顾客决定停止。在会话结束时,必须向用户显示总价。(这就是问题所在(

package courseworkitpl;

导入java.util.Scanner;

公开课课程

public static void main(String[] args) {
// TODO code application logic here
int choice,quantity;
double price,tax;
String answer;
int Macbook = 1;
int Dell = 2;
int ROG = 3;
int Alienware = 4;
int Razer = 5;
int MacbookCost = 300;
int DellCost = 250;
int ROGCost = 395;
int AlienwareCost = 400;
int RazerCost = 350;
tax=0.03;
Scanner keyboard = new Scanner(System.in);
Double totalSum = 0d;

do{

System.out.println("Welcome to my store, which computer do you like to purchase?");
System.out.println("1. Macbook");
System.out.println("2. Dell");
System.out.println("3. ROG");
System.out.println("4. Alienware");
System.out.println("5. Razer");
choice = keyboard.nextInt();
System.out.println("How much quantity you want to purchase?");
quantity = keyboard.nextInt();
if (choice == 1) {
price = (MacbookCost*0.03+(MacbookCost*quantity));
} else if (choice == 2) {
price = (DellCost*0.03+(DellCost*quantity));
} else if (choice == 3) {
price = (ROGCost*0.03+(ROGCost*quantity));
}else if (choice == 4) {
price = (AlienwareCost*0.03+(AlienwareCost*quantity));
}else if (choice == 5) {
price = (RazerCost*0.03+(RazerCost*quantity));
}        
else  {
price = 0;
System.out.println("invalid choice");
}
totalSum = totalSum + price;
System.out.print("Do you want to continue yes/no?");
answer = keyboard.next();
} while(answer.equalsIgnoreCase("yes"));
System.out.println("That will be $" + totalSum + " with 3% of tax.");
}

}

您必须将do..while条件更改为

Double totalSum = 0d;
do{
System.out.println("Welcome to my store, which computer do you like to purchase?");
System.out.println("1. Macbook");
System.out.println("2. Dell");
System.out.println("3. ROG");
System.out.println("4. Alienware");
System.out.println("5. Razer");
choice = keyboard.nextInt();
if (choice == 1) {
price = (MacbookCost*0.03+(MacbookCost));
} else if (choice == 2) {
price = (DellCost*0.03+(DellCost));
} else if (choice == 3) {
price = (ROGCost*0.03+(ROGCost));
}else if (choice == 4) {
price = (AlienwareCost*0.03+(AlienwareCost));
}else if (choice == 5) {
price = (RazerCost*0.03+(RazerCost));
}        
else  {
price = 0;
System.out.println("invalid choice");
}
totalSum = totalSum + price;
System.out.print("Do you want to continue yes/no?");
answer = keyboard.next();
} while(answer.equalsIgnoreCase("yes"));
System.out.println("That will be $" + totalSum + " with 3% of tax.");

对于对象,我们应该使用equals方法。

查看这些链接以了解更多详细信息链接1链路2

相关内容

最新更新