程序似乎没有进入
Program doesn't seem to enter while
我正在尝试制作一个输入硬币值以提供门票的程序,但它似乎并没有进入第一个,而当我 运行 首先它没有接受双打(例如 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;
}
}
}
你的条件,
while (synthiki=false){...}
应该是,
while (!synthiki){...}
第一个条件将false
赋值给synthiki
。由于 synthiki
是 boolean
,您可以直接在 while() {...}
中使用变量。此外,如果您必须检查 synthiki
的值,请使用 ==
而不是 =
。
赞:while(synthiki == false) {...}
您应该使用“==”进行比较,而不是“=”(赋值)。更改
while (synthiki=false)
至
while (synthiki == false)
=
是赋值运算符。它将右侧表达式的值分配给左侧变量,并 returns 它。如果要检查是否相等,应使用 ==
运算符:
while (synthiki == false) {
或者更好的是,因为它是一个布尔变量,所以不要将它的值与文字进行比较,而是直接对其求值:
while (!synthiki) {
我正在尝试制作一个输入硬币值以提供门票的程序,但它似乎并没有进入第一个,而当我 运行 首先它没有接受双打(例如 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;
}
}
}
你的条件,
while (synthiki=false){...}
应该是,
while (!synthiki){...}
第一个条件将false
赋值给synthiki
。由于 synthiki
是 boolean
,您可以直接在 while() {...}
中使用变量。此外,如果您必须检查 synthiki
的值,请使用 ==
而不是 =
。
赞:while(synthiki == false) {...}
您应该使用“==”进行比较,而不是“=”(赋值)。更改
while (synthiki=false)
至
while (synthiki == false)
=
是赋值运算符。它将右侧表达式的值分配给左侧变量,并 returns 它。如果要检查是否相等,应使用 ==
运算符:
while (synthiki == false) {
或者更好的是,因为它是一个布尔变量,所以不要将它的值与文字进行比较,而是直接对其求值:
while (!synthiki) {