我剛剛學習如何使用開關語句。我正在嘗試創建一個在雜貨店出售的物品的購物車。我必須創建一個程序,增加銷售物品的價值,然後打印最終價值。這是我到目前爲止,但是當我終止我的程序時,我得到一個錯誤,它不顯示最終值。開關語句和雙精度值
任何幫助非常感謝!
package Exercises;
import java.util.Scanner;
public class CalculatingSales {
public static void main(String[] args)
{
int total = 0;
int prod1Count = 0, prod2Count = 0, prod3Count = 0, prod4Count = 0, prod5Count = 0;
Scanner input = new Scanner(System.in);
System.out.printf("%s%n%s%n %s%n %s%n %s%n","Enter product number sold: " //prompt user to enter input
,"NOTE: Product number must be between 1 & 5",
"To terminate input... ",
"On UNIX/Linus?mac OS X type <Ctrl> d then press Enter",
"On Windows type <Ctrl> z then press Enter");
while(input.hasNext())
{
int item = input.nextInt();
total =+ item;
double product1;
double product2;
double product3;
double product4;
double product5;
switch (item + total)
{
case 1:
product1 = 2.98;
++prod1Count;
break;
case 2:
product2 = 4.50;
++prod2Count;
break;
case 3:
product3 = 9.98;
++prod3Count;
break;
case 4:
product4 = 4.49;
++prod4Count;
break;
case 5:
product5 = 6.87;
++prod5Count;
break;
default:
System.out.println("ERROR. You did not enter a value between 1 & 5!");
break;
}//end switch
}//end while
double shoppingCart = (double) total;
System.out.printf("%nTotal retail value of all products is: $%d", shoppingCart);
}
}
它應該是'total + = item;'。 – herohuyongtao 2014-09-27 01:50:26