我正在编写一个程序,其中生成两个随机数,然后用户选择一个数字。如果用户选择一个介于两个随机数之间的数字,请说“很好的猜测”;如果该用户没有选择介于两个随机数之间,请说“超出范围”。该程序提示用户输入“是”以再次播放。当用户停止播放时,它将返回正确猜测的百分比。
我一直在尝试使用while循环,我认为我的概念正确。
public static void main(String[] args) {
Scanner get = new Scanner(System.in);
Scanner get2 = new Scanner(System.in);
Scanner get3 = new Scanner(System.in);
Random ran = new Random();
int start = ran.nextInt(99) + 1;
int end = ran.nextInt(99) + 1;
int win = 0;
int loss = 0;
boolean keepGoing = false;
System.out.println("enter a number: ");
int num = get.nextInt();
displayGuessResults(start, end, num);
if (displayGuessResults(start, end, num) == true) {
win++;
}
if (displayGuessResults(start, end, num) == false) {
loss++;
}
System.out.println("enter 'yes' to continue: ");
String ans = get2.nextLine();
if (ans.equalsIgnoreCase("yes")) {
keepGoing = true;
} else {
keepGoing = false;
System.out.println("There are " + win + " guess(es) within the range");
double percent = (win / (win + loss));
System.out.println("Correct guess percentage is " + (percent*100) + "%");
}
while (keepGoing == true) {
start = ran.nextInt(99) + 1;
end = ran.nextInt(99) + 1;
displayGuessResults(start, end, getValidGuess(get));
if (displayGuessResults(start, end, num) == true) {
win++;
}
if (displayGuessResults(start, end, num) == false) {
loss++;
}
System.out.println("enter 'yes' to continue: ");
String ans2 = get2.nextLine();
if (ans2.equalsIgnoreCase("yes")) {
keepGoing = true;
} else {
keepGoing = false;
}
}
if (keepGoing == false) {
System.out.println("There are " + win + " guess(es) within the range");
double percent1 = (win / (win + loss));
System.out.println("Correct guess percentage is " + (percent1) + "%");
}
}
public static int getValidGuess(Scanner get3) {
int num = 0;
System.out.println("enter a number: ");
num = get3.nextInt();
if (num < 100 && num > 0) {
return num;
} else {
return -1;
}
}
public static boolean displayGuessResults(int start, int end, int num) {
boolean results = false;
int min = 0, max = 0;
if (start > end) {
max = start;
min = end;
} else if (start < end) {
min = start;
max = end;
}
if (min <= num && num <= max) {
System.out.println("You entered: " + num + " the nubmers are " + start + " and " + end);
System.out.println("good guess");
return results = true;
} else {
System.out.println("You entered: " + num + " the nubmers are " + start + " and " + end);
System.out.println("out of bounds");
return results = false;
}
}
预期结果:
输入数字:12
您输入了12,两个随机数分别是22和45
超出范围
输入是重新启动 是的
输入数字: 45
您输入了12,两个随机数分别是43和67 好猜
输入是重新启动 是的
输入数字: 98
您输入了98,两个随机数分别是23和87 越界
输入是重新启动 否
范围内有(1)个猜测
正确的猜测百分比是33.3%
实际结果:
输入数字: 43
您输入了:43个数字是33和30 越界 您输入了:43个数字是33和30 越界 您输入了:43个数字是33和30 越界
输入“是”以继续: 是的
输入数字: 34 您输入了:34个数字是81和98 越界 您输入了:43个数字是81和98 越界 您输入了:43个数字是81和98 越界
输入“是”以继续: 是的
输入数字: 98 您输入了:98的数字是96和90 越界 您输入了:43个数字是96和90 越界 您输入了:43个数字是96和90 越界
输入“是”以继续: 是的
输入数字: 25 您输入了:25个数字是54和40 越界 您输入了:43个数字是54和40 不错的猜测 您输入了:43个数字是54和40 好猜
输入“是”以继续: 否
该范围内有1个猜测 正确猜测百分比为0.0%
答案 0 :(得分:0)
本部分的第一部分
int num = get.nextInt();
displayGuessResults(start, end, num);
if (displayGuessResults(start, end, num) == true) {
win++;
}
if (displayGuessResults(start, end, num) == false) {
loss++;
}
每次调用displayGuessResults()时,您都将打印到控制台,我认为这不是您想要的行为。你应该做类似的事情
boolean result = displayGuessResults(start, end, num);
if (result) {
win++;
}
else {
loss++;
}
第二件事是程序中的逻辑流程,似乎您在while循环中有重复的代码,在破坏循环目的之前,您可以从keepGoing = true开始;并在while循环外删除代码或使用do-while。您的代码中有很多要修复的地方,您需要告诉我们您需要帮助的确切内容是什么。
答案 1 :(得分:0)
好吧,我想您在问题开始时就采用了正确的逻辑,但是没有遵循,您的displayGuessResults()
应该是这样的:
public static boolean displayGuessResults(int start, int end, int num) {
int min = 0, max = 0;
if (start > end) {
max = start;
min = end;
} else if (start < end) {
min = start;
max = end;
}
if (min <= num && num <= max) {
System.out.println("You entered: " + num + " the nubmers are " + min + " and " + max);
System.out.println("good guess");
return true;
} else {
System.out.println("You entered: " + num + " the nubmers are " + min + " and " + max);
System.out.println("out of bounds");
return false;
}
}
此外,在计算正确的猜测百分比时,除法时必须从int
转换为double
,否则它将取小数位。您必须这样做:
double percent1 = ((double) win) / (win + loss)