我是java的新手,我坚持使用非常简单的代码。我已经添加了while
条件,但eclipse仍然要求它。
错误:
语法错误,插入
while (Expression);
以完成BlockStatements
代码:
package assignment517;
import java.util.Scanner;
public class Products {
public static void main(String[] args) {
// TODO Auto-generated method stub
do{
Scanner input = new Scanner(System.in);
System.out.print("Enter the number of product");
int numberOfProduct=input.nextInt();
System.out.print("Enter the products sold");
int productsSold=input.nextInt();
double pprice = 1.0;
switch(numberOfProduct){
case 1: {
System.out.print("Price of total products sold");
System.out.print (pprice= 2.98*productsSold);
break;
}
case 2: {
System.out.print("Price of total products sold");
System.out.print(pprice= 4.50*productsSold);
break;
}
case 3: {
System.out.print("Price of total products sold");
System.out.print(pprice= 9.98*productsSold);
break;
}
case 4: {
System.out.print("Price of total products sold");
System.out.print(pprice= 4.49*productsSold);
break;
}
case 5:{
System.out.print("Price of total products sold");
System.out.print(pprice= 6.87*productsSold);
break;
}
default:{
System.out.print("invalid value");
}
}while(numberOfProduct !=6);
}
}
答案 0 :(得分:1)
您的while
放错地方了 - 它应该在}
的关闭do
之后,而不是switch
。请注意,由于它依赖于numberOfProduct
变量,因此您需要将其定义移到do-while循环之外。