System.out.println("购买物品\t" + "单价\t" + "个数\t" + "金额");
Scanner in = new Scanner(System.in);
String String1 ;
int a = 0;
int b = 0;
int c = 0;
double sum=0;
do {
System.out.println("请选择你购买的物品");
String aString = in.next();
if (aString.equals("上衣")) {
System.out.println("请选择购买的数量:");
int num1 = in.nextInt();
a = num1 * 245;
System.out.println("上衣的金额为" + a);
} else if (aString.equals("网球鞋")) {
System.out.println("请选择购买的数量:");
int num2 = in.nextInt();
b = 570 * num2;
System.out.println("网球鞋的金额为:" + b);
} else if (aString.equals("网球拍")) {
System.out.println("请选择购买的数量:");
int num3 = in.nextInt();
c = 320 * num3;
System.out.println("网球拍的金额为:" + c);
} else {
System.out.println("输入错误,请重新输入。");
}
System.out.println("是否继续输入Y/N");
String1 = in.next();
} while (String1.equals("Y"));
sum=a+b+c;
System.out.println("总金额为:" +sum );
}
}
int a = 0;
int b = 0;
int c = 0;
这一部分可以简化为int a=0;
再把sum=sum+a;放进do-while循环体里面,就可以自动计算总价了。