1
我是初学者,当谈到Java的。我有一个简单的Java编程任务。这里是该计划的基础:如何添加和多条语句在while循环中的Java
“最佳购买是接受电子设备和配件的在线订单,他们开发了一个程序,允许用户从一个项目菜单中选择购买的设备。一个程序允许用户(客户)选择要购买的物品,计算订购物品的价格,并向客户显示该信息的收据(假设销售税率为6.5%)“
您的程序应提示用户(客户)输入以下信息:
- 客户名称 台
- 电子设备及配件购买
然后程序输出以下信息:
- 客户姓名
- 项目的总数排序
- 价格的菜单项目前的营业税
- 营业税
- 总额由于
这里是我的问题是如何,如果根据什么数字用户选择我里面添加我的嵌套的值是多少?
例句:用户选择1,2,3,10(要退出环路)
这里是我的代码:
import java.util.Scanner;
public class OnlinePurchase {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
//Declare Variables
Scanner input = new Scanner(System.in);
String sCustomerName =""; //Stores the Customer's Name
int nSelection = 0; //Stores the value entered by the user
int nSum = 0; //Stores sum of values entered
int nCount = 0; //Number of values entered
int nPrice = 0;
//Declare Constants
final int SENTINEL = 10; //Used to end loop
//Promt User to enter the their name.
System.out.print("Please enter the your name: ");
//Read Customer Name input from user
sCustomerName = input.nextLine();
//Print to Blank Line for spacing
System.out.println("\n");
//Print Purchase Menu
System.out.println("BEST PURCHASE PRODUCTS:");
System.out.println("1. Smartphone..........$249");
System.out.println("2. Smartphone Case.... $39");
System.out.println("3. PC Laptop...........$1149");
System.out.println("4. Tablet..............$349");
System.out.println("5. Tablet Case.........$49");
System.out.println("6. eReader.............$119");
System.out.println("7. PC Desktop..........$889");
System.out.println("8. LED Monitor.........$299");
System.out.println("9. Laser Printer.......$399");
System.out.println("10.Complete my order");
//Print to Blank Line for spacing
System.out.println("\n");
while (nSelection != SENTINEL) {
//Calculate sum
nSum = nPrice + nSum;
//Increment counter
nCount++; //or nCount = nCount + 1;
//Promt User to enter the an item from the menu.
System.out.print("Please enter item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
if (nSelection == 1) {
nPrice = 249;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 2) {
nPrice = 39;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 3) {
nPrice = 1149;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 4) {
nPrice = 349;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 5) {
nPrice = 49;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 6) {
nPrice = 119;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 7) {
nPrice = 899;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 8) {
nPrice = 299;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
else if (nSelection == 9) {
nPrice = 399;
//Promt User to enter the an item from the menu.
System.out.print("Please enter another item from the menu above: ");
//Read input from user for selected purchase
nSelection = input.nextInt();
}
}//end while user did not enter 10
//Print blank line
System.out.println();
//Print Thank You message
System.out.println("Thank you for ordering with Best Purchase,"+sCustomerName);
//Print number of integers entered
System.out.println("Total Items Ordered: " + nCount);
//Print number of integers entered
System.out.println("Total: $" + nSum);
}//end main class
}//end public class
Stack Overflow是不是一门功课完成的服务。 – redFIVE
首先,如果你想让用户选择多个项目,你必须在循环中运行你的逻辑。其次,不是'nPrice = 119;'你可以做'nPrice + = 119;' - 例如... – alfasin
@redFIVE你有没有完全阅读他的问题?他没有要求我们去做他的HW。你应该收回你的失败。 – alfasin