查看: 1675|回复: 2
|
求助 java code~
[复制链接]
|
|
各位大大好,我想请教各位大大,我的java code到底错在哪里呢?为什么只能记录到最后一个账单的总数呢?求各位大大指导,谢谢
import java.util.Scanner;
public class BizCafe
{
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int[] array = new int[10];
double totalPrice = 0;
double temp = 0;
int input = 0;
double quantity = 0;
double unitprice = 0;
int big = 0;
System.out.println(" WELCOME TO BIZ CAFE ");
System.out.println("----------MENU----------");
System.out.println("1. Grill Beef = RM 23.99");
System.out.println("2. Fish n Chips = RM 19.45");
System.out.println("3. Spghattei = RM 9.00");
System.out.println("4. Latte = RM 8.50");
System.out.println("5. Cheese Burger = RM 6.50");
System.out.println("6. Grill Octopus = RM 23.50");
System.out.println("7. Soft Drinks = RM 1.50");
System.out.println("8. Chicken Noodle = RM 12.50");
System.out.println("9. Calsberg = RM 15.50");
System.out.println("10.Water = RM 1.00");
while(true){
System.out.println("Enter a Dishes number: ");
input = scanner.nextInt();
if(input > big)
big = input;
if(input == -1)
break;
System.out.println("Enter quantity: ");
quantity = scanner.nextDouble();
if(input == 1 ){
unitprice = 23.99;
}
else if(input == 2 ){
unitprice = 19.45;
}
else if(input == 3 ){
unitprice = 9.00;
}
else if(input == 4 ){
unitprice = 8.50;
}
else if(input == 5 ){
unitprice = 6.50;
}
else if(input == 6 ){
unitprice = 23.50;
}
else if(input == 7 ){
unitprice = 1.50;
}
else if(input == 8 ){
unitprice = 12.50;
}
else if(input == 9 ){
unitprice = 15.50;
}
else if(input == 10 ){
unitprice = 1.00;
}
else
System.out.println("ERROR!");
}
double k = getTotal(unitprice, quantity);
System.out.println("Total Price = RM " + k);
System.out.println("\nEnjoy your dishes");
System.out.println("Thank You Please Come Back Again");
}
public static double getTotal(double unitprice, double quantity){
double total = 0;
total = unitprice * quantity;
return total;
}
}
|
|
|
|
|
|
|
|
发表于 21-12-2016 03:51 AM
|
显示全部楼层
什么叫做“只能记录到最后一个账单的总数”。
请给个例子。 |
|
|
|
|
|
|
|
发表于 24-2-2017 09:58 AM
|
显示全部楼层
首先,不是很鼓励你用if else if else if的方式。个人反而会推荐使用switch case来处理,或者是String-Double array来处理:
String[] arrItemName = new String[10] {"1. bla", "2. blabla", ..., "10. blablabla"};
double[] arrItemPrice = new double[10] {"0.00, 0.10, ..., 1.00};
然后你再使用
try {
unitPrice = arrItemPrice[input];
} catch (ArrayIndexOutOfBoundException AIOOBe) {
unitPrice = -1;
System.out.println("Error!");
}
仔细看了看,应该是你的while(true)关错地方了吧。
只有在你的input等于-1的时候,才会停止Loop,但是只要你的Loop一直还在,你的unitprice就和一直”变化“。
while(true) {
...balabala
/*你的一大堆if else*/
double k = getTotal(unitPrice, quantity);
/**Store your double k**/
}
System.out.print..... |
|
|
|
|
|
|
| |
本周最热论坛帖子
|