gpt4 book ai didi

java - 依次运行两个 while 循环以获取用户输入

转载 作者:行者123 更新时间:2023-11-30 07:38:31 24 4
gpt4 key购买 nike

在我的方法中,如果它首先通过,则需要用户输入,而需要进入第二个 while 循环,在第二个 while 中,如果条件失败,则需要重新运行循环,但不是重新运行本身,而是第一个 while正在执行。

//Method to read, validate and store postcode and purchase amount for N customers
public void addRecord(int customerCounter) throws IOException
{
int postcode;
double purchaseAmount;

int conform =0;

do{
BufferedReader breader= new BufferedReader(new InputStreamReader(System.in));
System.out.print("Postcode: ");
postcode= Integer.parseInt(breader.readLine());

if(4121 >postcode)
{
System.out.print("Postcode must be greater than 4121");
System.out.print("Try again: ");
}
else if(postcode > 4123)
{
System.out.print("Postcode must be less than 4123");
System.out.print("Try again: ");
}
else
{
conform = 1;
//System.out.println(conform);
}
}
while(4121 >postcode && postcode > 4123);



if(conform == 1)
{
System.out.println(conform);
do
{
BufferedReader breader2= new BufferedReader(new InputStreamReader(System.in));
breader2= new BufferedReader(new InputStreamReader(System.in));
System.out.println("Purchase Amount: ");
purchaseAmount= Double.parseDouble(breader2.readLine());

if(purchaseAmount < 60)
{
System.out.print("Purchase Amount must be greater than $60");
System.out.print("Try again: ");
continue;
}
else if(purchaseAmount > 500)
{
System.out.print("Purchase Amount must be greater than $500");
System.out.print("Try again: ");
}
}
while(purchaseAmount < 60 && purchaseAmount > 500);

}

}

最佳答案

您在 while 中的条件是错误的,它们将永远是错误的

postcode = 5000;
while(postcode < 4121 && postcode > 4123); -> while(false && true); -> false

postcode = 3000;
while(postcode < 4121 && postcode > 4123); -> while(true && false); -> false

使用 OR 代替 AND

while(postcode < 4121 || postcode > 4123);

第二次也是如此。

第一个while的第二次执行可能是因为你再次调用了addRecord

关于java - 依次运行两个 while 循环以获取用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35031224/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com