gpt4 book ai didi

java - 哨兵值在循环时不退出

转载 作者:行者123 更新时间:2023-11-29 07:59:49 24 4
gpt4 key购买 nike

//在下面的程序中,最初当用户看到指令时,他可以简单地输入哨兵退出while循环,但是,当你得到你想买的票的结果时,按999并没有退出程序

import java.io.*;

public class ManyTickets
{

public static void main (String [] args) throws IOException
{
String userInput;
String userInput2;
int intInput = 0;
int intInput2 = 0;
double total = 0.00;
BufferedReader ageInput = new BufferedReader (new InputStreamReader (System.in));
try{
System.out.println("Please enter your age, or press '999' to exit.");
userInput = ageInput.readLine();
intInput = Integer.parseInt (userInput);
while (intInput != 999)
{
if (intInput < 0 || intInput > 110)
System.out.println("Invalid entry, or your age seems a bit too high to enter buy these tickets");
else if (intInput <= 12)
{
total = total + 6;
System.out.println("The ticket cost for 1 ticket is " + total);
}
else if (intInput <= 64)
{
total = total + 11;
System.out.println("The ticket cost for 1 ticket is " + total);
}
else
{
total = total + 8;
System.out.println("The ticket cost for 1 ticket is $" + total);
}
System.out.println("So far, your tickets cost is: $" + total );
System.out.print("Would you like to buy more tickets? You can buy up to 1 more ticket per customer! If no press 999 to exit");
userInput = ageInput.readLine();
intInput2 = Integer.parseInt (userInput);
}
}catch (NumberFormatException e){
System.out.println("Please restart the program, and enter an integer instead!");
}
}
{
double total = 0.0;
System.out.println("Thank you, The total cost for the ticket is: $" + total);
System.out.println("Have a nice day!");
}
}

最佳答案

您使用了两个不同的变量,intInputintInput2。我不明白你为什么需要第二个(就在 catch block 之前)。只需重用 intInput,这是在 while 循环中根据标记值进行检查的那个。

关于java - 哨兵值在循环时不退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15080526/

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