为什么用户在Java中总是得到一个返回true? public boolean askQuestion(ArrayListcountries){ int randomNumber=this.getRandomPositionList(countries.size()); Application.Country country1=countries.get(随机数); int randomNumber1=this.getRandomPositionList(countries.size()); Application.Country country2=countries.get(随机数1); 哪个国家有更多的人口; System.out.println(“a)”+country1.name); System.out.println(“b)”+country2.name); 字符串userInput=scanner.nextLine(); 而(!userInput.matches(“[a-b]”)则{ System.out.println(“请输入a或b”); userInput=scanner.nextLine(); if(userInput.matches(“a”)){ 布尔值a=country1.population>country2.population; }否则{ 返回false; } if(userInput.matches(“b”)){ 布尔b=country1.population>country2.population; }否则{ 返回false; } } 返回true; }
我想问用户,在一个测验中,来自两个随机国家的更多人口是什么。每次用户使用a或b来填写答案时,都会得到一个true作为回报。我不知道怎么修理它。我还在if和else中使用了return语句。下面是固定代码。您的代码中有一个逻辑缺陷,不管发生什么,它总是返回true 您还检查了while循环中的条件,如果用户第一次输入为什么用户在Java中总是得到一个返回true? public boolean askQuestion(ArrayListcountries){ int randomNumber=this.getRandomPositionList(countries.size()); Application.Country country1=countries.get(随机数); int randomNumber1=this.getRandomPositionList(countries.size()); Application.Country country2=countries.get(随机数1); 哪个国家有更多的人口; System.out.println(“a)”+country1.name); System.out.println(“b)”+country2.name); 字符串userInput=scanner.nextLine(); 而(!userInput.matches(“[a-b]”)则{ System.out.println(“请输入a或b”); userInput=scanner.nextLine(); if(userInput.matches(“a”)){ 布尔值a=country1.population>country2.population; }否则{ 返回false; } if(userInput.matches(“b”)){ 布尔b=country1.population>country2.population; }否则{ 返回false; } } 返回true; },java,while-loop,return,population,Java,While Loop,Return,Population,我想问用户,在一个测验中,来自两个随机国家的更多人口是什么。每次用户使用a或b来填写答案时,都会得到一个true作为回报。我不知道怎么修理它。我还在if和else中使用了return语句。下面是固定代码。您的代码中有一个逻辑缺陷,不管发生什么,它总是返回true 您还检查了while循环中的条件,如果用户第一次输入a或b,该循环将永远不会运行 public boolean askQuestion(ArrayList < Application.Country > countrie
a
或b
,该循环将永远不会运行
public boolean askQuestion(ArrayList < Application.Country > countries) {
int randomNumber = this.getRandomPositionList(countries.size());
Application.Country country1 = countries.get(randomNumber);
int randomNumber1 = this.getRandomPositionList(countries.size());
Application.Country country2 = countries.get(randomNumber1);
System.out.println("Which country has a larger population? ");
System.out.println("a) " + country1.name);
System.out.println("b) " + country2.name);
String userInput = scanner.nextLine();
while (!userInput.matches("[a-b]")) {
System.out.println("Please enter an a or b");
userInput = scanner.nextLine();
if (userInput.matches("a")) {
boolean a = country1.population > country2.population;
} else {
return false;
}
if (userInput.matches("b")) {
boolean b = country1.population > country2.population;
} else {
return false;
}
}
return true;
}
public boolean askQuestion(ArrayListcountries){
int randomNumber=this.getRandomPositionList(countries.size());
Application.Country country1=countries.get(随机数);
int randomNumber1=this.getRandomPositionList(countries.size());
Application.Country country2=countries.get(随机数1);
哪个国家有更多的人口;
System.out.println(“a)”+country1.name);
System.out.println(“b)”+country2.name);
字符串userInput=scanner.nextLine();
而(!userInput.matches(“[a-b]”)则{
System.out.println(“请输入a或b”);
userInput=scanner.nextLine();
}
字符串答案=(country1.population>country2.population)?“a”:“b”;
if(userInput.matches(应答)){
返回true;
}
返回false;
}
您不返回a或b。您只需分配给它们,然后忽略它们并返回true。您需要将匹配项(“a”)
和匹配项(“b”)
签出while循环。感谢您的评论。你能再精确一点吗。我是初学者。您可以共享代码吗?您可以替换boolean a=country1.population>country2.population代码>带返回(country1.population>country2.population)代码>如果该条件是您应该返回的。如果第一个输入不正确,您当前只检查a或b。您需要将这些检查移出while循环,以便它们在while循环条件的计算结果为false之后发生。
public boolean askQuestion(ArrayList < Application.Country > countries) {
int randomNumber = this.getRandomPositionList(countries.size());
Application.Country country1 = countries.get(randomNumber);
int randomNumber1 = this.getRandomPositionList(countries.size());
Application.Country country2 = countries.get(randomNumber1);
System.out.println("Which country has a larger population? ");
System.out.println("a) " + country1.name);
System.out.println("b) " + country2.name);
String userInput = scanner.nextLine();
while (!userInput.matches("[a-b]")) {
System.out.println("Please enter an a or b");
userInput = scanner.nextLine();
}
String answer = (country1.population > country2.population) ? "a" : "b";
if (userInput.matches(answer)) {
return true;
}
return false;
}