double a = 10;// the 10 dollars to spend
double b = 0;// the cost of he food
double c;// final total
double d;// amount of sandwiches
c = a-b;
String Order;
Scanner sc = new Scanner(System.in);
这就是我想要做的,我的目标是获得三明治的数量并乘以它;当数字是2或更低时,她说一件事3或更高的另一件事。问题是它要求我将变量初始化为0,但显然d * 0仍为零。
System.out.println("That'll be 1.06 how many would you like?");
b = 1.06*d;
d = sc.nextDouble();
System.out.println(d <= 2 ?"Ok! Coming right up Sir!":"...Ok.... she probably thinks you're a fatso now.");
System.out.println("She hands you back "+(a-b)+" as your change");
break;
答案 0 :(得分:2)
问题在于:
System.out.println("That'll be 1.06 how many would you like?");
b = 1.06*d;
d = sc.nextDouble();
你还没有初始化d
,所以当你进行乘法运算时,你没有任何乘法。在将d
相乘之前,您需要将扫描仪的值加载到b
。
System.out.println("That'll be 1.06 how many would you like?");
d = sc.nextDouble();
b = 1.06*d;
答案 1 :(得分:0)
交换作业的顺序。
b = 1.06*d; // <-- what's d?
d = sc.nextDouble();
像
d = sc.nextDouble();
b = 1.06 * d;
此外,您应该选择更有意义的变量名称。