[英]Why is my java while loop continue printing with true status?
下面我有一个数组,我正在检查用户输入,猜测我钱包中的卡类型? 如果为真,则退出 while 循环,否则再次询问用户,直到匹配我的数组中的相同卡片类型。 我的问题是当用户正确猜测时,我仍然收到“卡类型不正确再试一次消息”。 有没有办法缩短我的代码或更好地纠正它?
public static void main(String[] args) {
String correctGuess = validateGuessedCardType();
System.out.println(correctGuess);
}
private static final String[] cardsTypesInWallet = {
"DBS",
"POSB",
"AMEX",
"Standard Charted"
};
private static String validateGuessedCardType() {
boolean correctGuess = false;
String guessedCard = "";
while (!correctGuess) {
in = new Scanner(System.in);
System.out.println("Guess a card in my wallet");
guessedCard = in.nextLine();
for (int i = 0; i < cardsTypesInWallet.length; i++) {
if (cardsTypesInWallet[i].equals(guessedCard)) {
correctGuess = true;
}
}
System.out.println("Card Type is incorrect");
System.out.println("try again");
}
return guessedCard;
}
因为循环没有终止,它一直持续到结束。 如果匹配,您可以从for
循环返回值
private static String validateGuessedCardType() {
String guessedCard = "";
while (true) {
in = new Scanner(System.in);
System.out.println("Guess a card in my wallet");
guessedCard = in.nextLine();
for (int i = 0; i < cardsTypesInWallet.length; i++) {
if (cardsTypesInWallet[i].equals(guessedCard)) {
return guessedCard;
}
}
System.out.println("Card Type is incorrect");
System.out.println("try again");
}
}
因为您没有将这些 System.out 行放在适当的 if 子句中。
请注意,您正在运行 for 循环,但实际上并不确定循环需要运行的迭代次数 - 因为定义是“继续遍历所有循环,除非其中一个正确。如果正确,则停止”。
这将是进行这两个修复的结果代码。
还要记住,不建议使用中断操作数,因为它违背了结构化编程范式推理——它“破坏”了结构。 因此,即使它可以用于改进您的代码,我建议您改为遵循此建议。
private static String validateGuessedCardType() {
boolean correctGuess = false;
String guessedCard = "";
while (!correctGuess) {
in = new Scanner(System.in);
System.out.println("Guess a card in my wallet");
guessedCard = in.nextLine();
int i=0;
while(i < cardsTypesInWallet.length && !correctGuess) {
if (cardsTypesInWallet[i].equals(guessedCard)) {
correctGuess = true;
}
else i++;
}
if(!correctGuess){
System.out.println("Card Type is incorrect"); System.out.println("try again");
}
}
return guessedCard;
}
尝试这个
public class Test1 {
static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
String correctGuess = validateGuessedCardType();
System.out.println(correctGuess);
}
private static final String[] cardsTypesInWallet = {
"DBS",
"POSB",
"AMEX",
"Standard Charted"
};
private static String validateGuessedCardType() {
boolean correctGuess = false;
String guessedCard = "";
while (!correctGuess) {
System.out.println("Guess a card in my wallet");
guessedCard = in.nextLine();
for (int i = 0; i < cardsTypesInWallet.length; i++) {
if (cardsTypesInWallet[i].equals(guessedCard)) {
correctGuess = true;
}
}
if(correctGuess) {
System.out.println("Card Type is correct");
} else {
System.out.println("Card Type is incorrect");
System.out.println("try again");
}
}
return guessedCard;
}
}
private static String validateGuessedCardType() {
boolean correctGuess = false;
String guessedCard = "";
outer:
while (!correctGuess) {
in = new Scanner(System.in);
System.out.println("Guess a card in my wallet");
guessedCard = in.nextLine();
for (int i = 0; i < cardsTypesInWallet.length; i++) {
if (cardsTypesInWallet[i].equals(guessedCard)) {
break outer;
}
}
System.out.println("Card Type is incorrect");
System.out.println("try again");
}
return guessedCard;
}
OR
private static final List<String> cardsTypesInWallet = Arrays.asList{
"DBS",
"POSB",
"AMEX",
"Standard Charted"
};
private static String validateGuessedCardType() {
boolean correctGuess = false;
String guessedCard = "";
while (!correctGuess) {
in = new Scanner(System.in);
System.out.println("Guess a card in my wallet");
guessedCard = in.nextLine();
if (cardsTypesInWallet.contains(guessedCard){
correctGuess = true;
}
else {
System.out.println("Card Type is incorrect");
System.out.println("try again");
}
}
return guessedCard;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.