gpt4 book ai didi

java - 重新提示用户在java中输入有效的输入

转载 作者:太空宇宙 更新时间:2023-11-04 12:44:07 25 4
gpt4 key购买 nike

我有一个请求用户输入的代码,它运行得很好,但我想重新提示用户输入有效的响应,而不是继续运行。

代码如下:

import java.io.*;
public class SkiLodge {

public static void main(String[] args) throws Exception
{

InputStreamReader ui = new InputStreamReader (System.in);
BufferedReader inputReader = new BufferedReader(ui);

System.out.println("Select Membership Type: (Gold/Silver/Bronze)");
String membership = inputReader.readLine();

double x = 0d;

if (membership.equals("Gold"))
System.out.println("Gold Membership Selected: £" + (x = 750));
else if (membership.equals("Silver"))
System.out.println("Silver Membership Selected: £" + (x = 450));
else if (membership.equals("Bronze"))
System.out.println("Bronze Membership Selected: £" + (x = 250));
else System.out.println("Invalid Membership");



System.out.println("Do you require snow tyre rental? (yes/no)");
String tyreRental = inputReader.readLine();


if (tyreRental.equals("yes"))
System.out.println("The total so far with tyre rental: £" + (x = x * 1.1));
else if (tyreRental.equals("no"))
System.out.println("The total so far without tyre rental: £" + x);
else System.out.println("Invalid response");

System.out.println("Would you like to reserve parking? (yes/no)");
String parking = inputReader.readLine();

if (parking.equals("yes"))
System.out.println("Membership total: £" + (x = x * 1.08));
else if (parking.equals("no"))
System.out.println("Membership total: £" + x);
else System.out.println("Invalid response");
}

}

例如,如果用户输入 Gold,程序会打印出 750,如果没有,则会打印出无效的成员(member)资格并要求提供有效的成员(member)资格。

以下问题也是如此,因此程序仅接受"is"和“否”作为有效答案。

提前致谢

最佳答案

您可以使用 do-while 循环。

do{
//Some code here

//if block
else { // last else block.
System.out.println("Invalid Membership");
continue; // This will skip the rest of the code and move again to the beginning of the while loop.
}

//More code

break; // If the condition was correct, then you need not worry. This statement will pull out of the loop.
}while(true);

您可以修改它以在任何有此类要求的地方放置 do-while 循环。

这将使您的整个代码:

public static void main(String[] args) throws Exception
{

InputStreamReader ui = new InputStreamReader (System.in);
BufferedReader inputReader = new BufferedReader(ui);
String membership, tyreRental,parking;
double x = 0d;
do
{
System.out.println("Select Membership Type: (Gold/Silver/Bronze)");
membership = inputReader.readLine();

if (membership.equals("Gold"))
System.out.println("Gold Membership Selected: £" + (x = 750));
else if (membership.equals("Silver"))
System.out.println("Silver Membership Selected: £" + (x = 450));
else if (membership.equals("Bronze"))
System.out.println("Bronze Membership Selected: £" + (x = 250));
else{
System.out.println("Invalid Membership");
continue;
}
break;
}while(true);

do{
System.out.println("Do you require snow tyre rental? (yes/no)");
tyreRental = inputReader.readLine();

if (tyreRental.equals("yes"))
System.out.println("The total so far with tyre rental: £" + (x = x * 1.1));
else if (tyreRental.equals("no"))
System.out.println("The total so far without tyre rental: £" + x);
else{ System.out.println("Invalid response"); continue;}
break;
}while(true);

do{
System.out.println("Would you like to reserve parking? (yes/no)");
parking = inputReader.readLine();

if (parking.equals("yes"))
System.out.println("Membership total: £" + (x = x * 1.08));
else if (parking.equals("no"))
System.out.println("Membership total: £" + x);
else {System.out.println("Invalid response"); continue;}
break;
}while(true);
}

关于java - 重新提示用户在java中输入有效的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36516266/

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