简体   繁体   English

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

[英]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);

And here is what i'm trying to do my objective is to get the number of sandwiches and multiply it; 这就是我要达到的目的,我的目的是获得三明治的数量并乘以它。 while the number is 2 or lower she says one thing 3 or higher another. 当数字等于或小于2时,她说一件事等于或大于3。 The problem is it's asking me to initialize the variable to 0 but obviously d*0 is still zero. 问题是它要我将变量初始化为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;

The problem is here: 问题在这里:

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

You haven't initialized d , so when you do your multiplication you are multiplying nothing. 您尚未初始化d ,所以当您进行乘法运算时,您没有乘积。 You need to load the scanner's value into d before you multiply b . 在将b乘以之前,需要将扫描仪的值加载到d

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

Swap the order of your assignments. 交换作业的顺序。

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

like 喜欢

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

Also, you should pick more meaningful variable names. 另外,您应该选择更有意义的变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM