[英]How would I add a loop to this Java program that does a validation check on the user's input?
所以我試圖向這個程序添加一個循環,如果用戶輸入的顏色不是紅色、綠色、藍色、橙色或黃色,它將向用戶顯示一條錯誤消息。 我只是不確定該怎么做。 我試過在userColorChoice
方法中添加一個 while 循環,但它只會創建一個無限循環 - 所以我做錯了什么。 謝謝!
import java.util.Scanner;
import java.util.Random;
public class ESP
{
public static void main(String[] args)
{
int correct = 0, incorrect = 0;
for(int i = 1; i <= 10; i++)
{
if(userColorChoice().equalsIgnoreCase(computerColorChoice()))
{
System.out.println("You guessed correctly!");
correct++;
}
else
{
System.out.println("Sorry, your guess was wrong!");
incorrect++;
}
}
System.out.println("Amount of times you guessed correctly: " + correct);
System.out.println("Amount of times you guess incorrectly: " + incorrect);
}
public static String userColorChoice()
{
Scanner keyboard = new Scanner(System.in);
System.out.print("Choose either red, green, blue, orange or yellow! ");
String userColorChoice = keyboard.nextLine();
System.out.println("You chose: " + userColorChoice);
return userColorChoice;
}
public static String computerColorChoice()
{
String randomColor;
int randomNumber;
Random random = new Random();
randomNumber = random.nextInt(4);
switch (randomNumber)
{
case 0:
randomColor = "Red";
break;
case 1:
randomColor = "Green";
break;
case 2:
randomColor = "Blue";
break;
case 3:
randomColor = "Orange";
break;
case 4:
randomColor = "Yellow";
break;
default:
randomColor = " ";
}
System.out.println("The computer chose: " + randomColor);
return randomColor;
}
}
我建議你介紹一個enum
COLOR
還要將computerColorChoice
方法更改為如下所示,這樣您就不必在每次添加新顏色時都添加代碼
我將這一行添加到COLOR.valueOf(capitaliseFirstLetter(userColorChoice));
如果userColorChoice
不是 colors 之一,這將拋出正在 main 方法 catch 塊中處理的IllegalArgumentException
public static void main(String[] args) {
int correct = 0, incorrect = 0, i = 0;
while (i < 10) {
try {
if (userColorChoice().equalsIgnoreCase(computerColorChoice())) {
System.out.println("You guessed correctly!");
correct++;
} else {
System.out.println("Sorry, your guess was wrong!");
incorrect++;
}
i++;
} catch (IllegalArgumentException e) {
System.out.println("invalid choice");
}
}
System.out.println("Amount of times you guessed correctly: " + correct);
System.out.println("Amount of times you guess incorrectly: " + incorrect);
}
public static String userColorChoice() {
Scanner keyboard = new Scanner(System.in);
System.out.print("Choose either red, green, blue, orange or yellow! ");
String userColorChoice = keyboard.nextLine();
COLOR.valueOf(capitaliseFirstLetter(userColorChoice));
System.out.println("You chose: " + userColorChoice);
return userColorChoice;
}
private static String capitaliseFirstLetter(String choice) {
return choice.substring(0, 1).toUpperCase() + choice.toLowerCase().substring(1);
}
public static String computerColorChoice() {
COLOR[] colors = COLOR.values();
return colors[new Random().nextInt(colors.length)].name();
}
enum COLOR {
Red, Green, Blue, Orange, Yellow;
}
我會使用List<String>
來保存 colors。這將使用戶驗證和計算機選擇變得更加容易:
import java.util.*;
class Main {
public static Random random = new Random();
public static Scanner keyboard = new Scanner(System.in);
public static List<String> colors = Arrays.asList("RED", "GREEN", "BLUE", "ORANGE", "YELLOW");
public static void main(String[] args)
{
int correct = 0, incorrect = 0;
for(int i = 1; i <= 10; i++)
{
if(userColorChoice().equalsIgnoreCase(computerColorChoice()))
{
System.out.println("You guessed correctly!");
correct++;
}
else
{
System.out.println("Sorry, your guess was wrong!");
incorrect++;
}
}
System.out.println("Amount of times you guessed correctly: " + correct);
System.out.println("Amount of times you guess incorrectly: " + incorrect);
}
public static String userColorChoice()
{
String userColorChoice;
do {
System.out.print("Choose either red, green, blue, orange or yellow! ");
userColorChoice = keyboard.nextLine().toUpperCase();;
} while (colors.indexOf(userColorChoice) == -1);
System.out.println("You chose: " + userColorChoice);
return userColorChoice;
}
public static String computerColorChoice()
{
String randomColor = colors.get(random.nextInt(colors.size()));
System.out.println("The computer chose: " + randomColor);
return randomColor;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.