java程序在运行时似乎没有进入
我试图制作一个程序,输入硬币值,以便给票,但它似乎没有进入第一,而当我运行它首先不接受双倍(例如0.1或0.5),无论你输入什么数字,它说这里是你的票和结束!代码有什么问题
import acm.program.*;
public class tickets extends ConsoleProgram {
public static double eisitirio = 1.2;
public void run(){
double nomisma=readInt("Insert coins and then press 0: ");
boolean synthiki=false;
double poso=0;
while (synthiki=false){
while (nomisma != 0){
if ((nomisma==0.1)||(nomisma==0.2)||(nomisma==0.5)||(nomisma==1)||(nomisma==2)||(nomisma==5)){
poso=poso+nomisma;
}else {
System.out.println("You did not insert a supported coin, please insert another one");
}
nomisma=readInt("Insert coins and then press 0: ");
}
if (poso < eisitirio){
System.out.println("You did not insert enough money, please insert more coins");
}else {
synthiki=true;
}
}
println("Here is your ticket");
poso=poso-eisitirio;
if ((poso/5) > 0){
println("You have change: 5 euros");
poso = poso-5;
}
if ((poso/2) > 0){
println("You have change: 2 euros");
poso = poso-2;
}
if ((poso/1) > 0){
println("You have change: 1 euros");
poso = poso-1;
}
if ((poso/0.5) > 0){
println("You have change: 50 cents");
poso = poso-0.5;
}
if ((poso/0.2) > 0){
println("You have change: 20 cents");
poso = poso-0.2;
}
if ((poso/0.1) > 0){
println("You have change: 10 cents");
poso=poso-0.1;
}
}
}
# 1 楼答案
=
是赋值运算符。它将右侧表达式的值赋给左侧变量,并返回它。如果要检查是否相等,应使用==
运算符:或者更好的是,因为它是一个布尔变量,所以不要将其值与文字进行比较,而是直接计算:
# 2 楼答案
你的条件
应该是
第一个条件将
false
分配给synthiki
。因为synthiki
是一个boolean
,所以可以直接在while() {...}
中使用变量。此外,如果必须检查synthiki
的值,请使用==
而不是=
比如:
while(synthiki == false) {...}
# 3 楼答案
你应该使用“==”进行比较,而不是“=”(作业)。改变
到