我的条件不起作用,我是初学者

My conditional isn't working, i'm a beginner programmer

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);

这就是我正在尝试做的 objective 是获取三明治的数量并将其相乘;当数字是 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;

问题出在这里:

System.out.println("That'll be 1.06 how many would you like?");
b = 1.06*d;
d = sc.nextDouble();

您还没有初始化 d,所以当您进行乘法运算时,您什么也没乘。在乘以 b.

之前,您需要将扫描仪的值加载到 d
System.out.println("That'll be 1.06 how many would you like?");
d = sc.nextDouble();
b = 1.06*d;

调换作业顺序。

b = 1.06*d; // <-- what's d?
d = sc.nextDouble();

喜欢

d = sc.nextDouble();
b = 1.06 * d;

此外,您应该选择更有意义的变量名称。