英文:
invalid input from the user using loop
问题
import java.util.Scanner;
import java.util.Random;
public class ShowWhatYouKnow {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rd = new Random();
String ready;
int guess;
int[] dice = {1, 2, 3, 4, 5, 6};
System.out.println("Welcome to the dice game!");
System.out.println();
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready?");
ready = key.nextLine();
// Implementing a loop to handle invalid input
while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no")) {
System.out.println("Invalid input!");
System.out.println();
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready?");
ready = key.nextLine();
}
if (ready.equalsIgnoreCase("yes")) {
System.out.println("Let The Game Begin!!");
System.out.println("-----------------------------------------");
// Rest of the code for the game
} else if(ready.equalsIgnoreCase("no")) {
System.out.println("You Exited The Program!! Bye!");
System.out.println("-----------------------------------------");
System.out.println();
}
}
}
英文:
I am trying to make a while loop when the input from the user is invalid meaning it is not "yes" or "no" so that the question would appear again. i would appreciate it if you could help me out here!!! thanks(i have deleted most of the code except for the problem because it was too long !!)
code:
import java.util.Scanner;
import java.util.Random;
public class ShowWhatYouKnow {
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rd = new Random();
String ready;
int guess;
int[] dice = {1, 2, 3, 4, 5, 6};
System.out.println("Welcome to dice game!");
System.out.println("");
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
// trying to do a do while here so if the user inputs the invalid option this would accur again and again
/*
System.out.println("invalid input !");
System.out.println("");
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
*/
if (ready.equalsIgnoreCase("yes")) {
System.out.println("Let The Game Begin!!");
System.out.println("-----------------------------------------");
}
} else if(ready.equalsIgnoreCase("no")){
System.out.println("You Exited The Program!! Bye!");
System.out.println("-----------------------------------------");
System.out.println("");
}
}
}
答案1
得分: 0
使用一个无限循环,并在输入正确时中断它
while (true) {
System.out.println("猜一个1-6之间的数字,赢取$50奖金!!");
System.out.println("你准备好了吗?");
ready = key.nextLine();
if (ready.equalsIgnoreCase("是")) {
System.out.println("游戏开始!");
System.out.println("-----------------------------------------");
break;
} else if (ready.equalsIgnoreCase("否")) {
System.out.println("你退出了程序!再见!");
System.out.println("-----------------------------------------");
System.out.println("");
break;
} else {
System.out.println("无效的输入!");
}
}
英文:
Use an infinite loop and break it when the input is correct
while (true) {
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
if (ready.equalsIgnoreCase("yes")) {
System.out.println("Let The Game Begin!!");
System.out.println("-----------------------------------------");
break;
} else if (ready.equalsIgnoreCase("no")) {
System.out.println("You Exited The Program!! Bye!");
System.out.println("-----------------------------------------");
System.out.println("");
break;
} else {
System.out.println("invalid input !");
}
}
答案2
得分: 0
你可以使用 [Do-while 循环][1] 来解决你的问题:如果用户输入不是 "yes" 或 "no",问题将会再次出现。你可以像这样编写主方法:
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rd = new Random();
String ready;
int guess;
int[] dice = {1, 2, 3, 4, 5, 6};
System.out.println("欢迎来到骰子游戏!");
do {
System.out.println("");
System.out.println("猜一个从1到6的数字,赢取 $50!!");
System.out.println("你准备好了吗?");
ready = key.nextLine();
} while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no"));
if (ready.equalsIgnoreCase("yes")) {
System.out.println("让游戏开始吧!");
System.out.println("-----------------------------------------");
} else if (ready.equalsIgnoreCase("no")) {
System.out.println("你退出了程序!再见!");
System.out.println("-----------------------------------------");
System.out.println("");
}
}
[1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
<details>
<summary>英文:</summary>
You could use [Do-while loop][1] to solve your problem: the question will appear again if the input from user is not "yes" or "no". You could write the main method like this:
public static void main(String[] args) {
Scanner key = new Scanner(System.in);
Random rd = new Random();
String ready;
int guess;
int[] dice = {1, 2, 3, 4, 5, 6};
System.out.println("Welcome to dice game!");
do {
System.out.println("");
System.out.println("Guess a number between 1-6 and win $50!!");
System.out.println("Are you ready??");
ready = key.nextLine();
} while (!ready.equalsIgnoreCase("yes") && !ready.equalsIgnoreCase("no"));
if (ready.equalsIgnoreCase("yes")) {
System.out.println("Let The Game Begin!!");
System.out.println("-----------------------------------------");
} else if (ready.equalsIgnoreCase("no")) {
System.out.println("You Exited The Program!! Bye!");
System.out.println("-----------------------------------------");
System.out.println("");
}
}
[1]: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论