Java 在doAgain方法中的do-while循环中获取错误
在我的Java 在doAgain方法中的do-while循环中获取错误,java,Java,在我的doAgain方法中的do-while循环中获取错误。不知道为什么。尝试使用其他布尔方法检查用户输入中的错误: import java.util.Scanner; import java.util.Random; public class Lottery { public static void main(String[] args) { do { int digits = getDigits(); System.o
doAgain
方法中的do-while循环中获取错误。不知道为什么。尝试使用其他布尔方法检查用户输入中的错误:
import java.util.Scanner;
import java.util.Random;
public class Lottery {
public static void main(String[] args) {
do {
int digits = getDigits();
System.out.println(createNumber(digits));
} while (doAgain() != false);
}
public static int getDigits() {
Scanner kb = new Scanner(System.in);
System.out.println("How many digits do you want the num to be?");
int digits = kb.nextInt();
return digits;
}
public static String createNumber(int digits) {
Random r = new Random();
StringBuilder sb = new StringBuilder(digits);
for (int i = 0; i < digits; i++)
sb.append((char) ('0' + r.nextInt(10)));
return sb.toString();
}
public static boolean doAgain() {
do {
Scanner kb = new Scanner(System.in);
String answer;
System.out.println("Do you want to do this again? Enter yes to continue or no to quit");
answer = kb.nextLine();
if (answer.equals("yes"))
return true;
else
return false;
} while (incorrect(answer) != false);
}
public static boolean incorrect(String answer) {
if (!answer.equals("yes") || !answer.equals("no"))
return true;
else
return false;
}
}
import java.util.Scanner;
导入java.util.Random;
公共类彩票{
公共静态void main(字符串[]args){
做{
int digits=getDigits();
System.out.println(createNumber(数字));
}while(doAgain()!=false);
}
公共静态整型getDigits(){
扫描仪kb=新扫描仪(System.in);
System.out.println(“您希望num为多少位?”);
int digits=kb.nextInt();
返回数字;
}
公共静态字符串createNumber(整数位数){
随机r=新随机();
StringBuilder sb=新的StringBuilder(数字);
对于(int i=0;i
您应该声明String answer=“”
;内置doAgain
函数。否则,您将在此行中得到找不到符号错误
while (incorrect(answer) != false);
将doAgain函数修改为此
public static boolean doAgain() {
String answer="";
do {
Scanner kb = new Scanner(System.in);
// String answer; // remove this
System.out.println("Do you want to do this again? Enter yes to continue or no to quit");
answer = kb.nextLine();
if (answer.equals("yes")) {
return true;
} else {
return false;
}
} while (incorrect(answer) != false);
}
以下是您犯下的一些错误:
!回答:等于(“是”)||!回答。等于(“否”)
始终返回true
中的答案
而(不正确(答案)!=false)代码>超出范围。您需要声明String-answer=“”代码>在do while循环之前
public static boolean doAgain() {
Scanner kb = new Scanner(System.in);
String answer = "";
do {
System.out.println("Do you want to do this again? Enter yes to continue or no to quit");
answer = kb.nextLine();
} while (incorrect(answer)); // Better not use double negative
if (answer.equals("yes"))
return true;
else
return false;
}
public static boolean incorrect(String answer) {
if (answer.equals("yes") || answer.equals("no"))
return false; // answer is correct
else
return true; // answer is incorrect
}
你能详细说明一下出错的原因吗?欢迎来到Stackoverflow,请阅读。特别注意。确保用正确的标签(编程语言、相关技术等)标记问题。你在发布一个好的问题上投入的精力越多:一个容易阅读、理解的问题,而且这个问题越容易吸引相关的人,你得到帮助的速度就越快。祝你好运乍一看,我猜你的
会做…而是不相关的,因为你在检查条件之前会返回true或false。你能试着不要实例化那么多扫描器?您应该关闭它们,但在您的情况下,您将关闭系统。在中也。。。破坏代码。使用else if检查您的错误条件,否则代码将根本不会进入while循环。。。