我制作了一个Java程序 - 这个程序会随机选择0到7之间的3个数字,我们必须猜测它们是什么。要查看所有数字,我想验证输入。如果用户输入的数字大于7,则会再次询问号码,同时忽略用户先前输入的号码。
代码已成功编译,但是当我运行它并选择大于7的数字时,它不会提示用户输入新号码。可能导致错误的原因是什么?我该如何解决?
import java.io.*;
import java.util.*;
import java.lang.*;
class GameHelper {
public String getUserInput(String prompt) {
String inputLine = null;
System.out.print(prompt + " ");
try {
BufferedReader is = new BufferedReader(new InputStreamReader(System.in));
inputLine = is.readLine();
if(inputLine.length() == 0) {
return null;
}
} catch(IOException e) {
System.out.println("IOException: " + e);
}
return inputLine;
}
}
class DotGame {
private int PlayerGuess;
private int NumberOfGuesses = 0;
private int[] SelectedNumbers;
private int NumberOfHits = 0;
public void setPlayerGuess(String PlayerGuess1) {
int PlayerGuess2 = Integer.parseInt(PlayerGuess1);
if(PlayerGuess2 < 8) {
PlayerGuess = PlayerGuess2;
} else {
System.out.println("enter number smaller than 8 ");
}
}
public int getPlayerGuess() {
return PlayerGuess;
}
public void setSelectedNumbers(int[] selected) {
int totalArrayValue = selected[0] + selected[1] + selected[2];
if(totalArrayValue < 21) {
SelectedNumbers = selected;
} else {
System.out.println("values in array are more than they should be ");
}
}
public String checkPlayerResult() {
boolean big = true;
NumberOfGuesses++;
String result = "miss";
for(int number : SelectedNumbers) {
if(PlayerGuess == number) {
result = "hit";
NumberOfHits++;
break;
}
}
if(NumberOfHits == SelectedNumbers.length) {
result = "killed";
}
System.out.println(result);
return result;
}
public void GameScore() {
int Score = NumberOfHits * 100 / NumberOfGuesses;
System.out.println("Number of hits " + NumberOfHits);
System.out.println("Number of guesses " + NumberOfGuesses);
System.out.println("you scored " + Score);
}
}
public class DotGameTester2 {
public static void main(String[] args) {
GameHelper helper = new GameHelper();
int random = (int) (Math.random() * 7);
int random1 = (int) (Math.random() * 7);
int random2 = (int) (Math.random() * 7);
DotGame game = new DotGame();
int[] selectnumb = { random, random1, random2 };
boolean isAlive = true;
int numOfGuesses = 0;
int test = 0;
game.setSelectedNumbers(selectnumb);
while(isAlive == true) {
do {
String guess = helper.getUserInput("enter a number ");
game.setPlayerGuess(guess);
test = game.getPlayerGuess();
} while(test > 8);
String result1 = game.checkPlayerResult();
numOfGuesses++;
if(result1 == "killed") {
isAlive = false;
}
}
System.out.println("you took " + numOfGuesses + " to guess");
game.GameScore();
}
}
答案 0 :(得分:2)
while(test > 8)
应该是
while(test >= 8)
此外,当您插入大于7的数字时,仍会将之前插入的数字返回test
,因此您检查的数字是否错误。
为了解决这个问题,我会像这样编辑:
PlayerGuess = PlayerGuess2;
if (PlayerGuess2 < 8) {
System.out.println("enter number smaller than 8 ");
}
另一种解决方案是编辑这样的代码:
do {
game.resetGuess();
String guess = helper.getUserInput("enter a number ");
game.setPlayerGuess(guess);
test = game.getPlayerGuess();
} while (test >= 8);
在PlayerGuess = 8
resetGuess()
答案 1 :(得分:0)
如果您输入11
作为猜测,那么会发生什么:
String guess = helper.getUserInput("enter a number "); // guess == 11
game.setPlayerGuess(guess /* 11 */);
深入研究setPlayerGuess
:
if(PlayerGuess2 < 8) { // False, since 11 > 8
PlayerGuess = PlayerGuess2; // skip
} else { // True
System.out.println("enter number smaller than 8 "); // This line gets executed
}
请注意如何定义private int PlayerGuess;
,其中PlayerGuess
默认为0
。考虑到这一点......
test = game.getPlayerGuess();
这只会返回PlayerGuess
0
。
现在,您在while
循环中运行此操作,该循环在test > 8
时执行,但此时test
为0
,因此循环终止
修复方法是将setPlayerGuess
更改为此,因此您每次输入数字时都会设置猜测:
public void setPlayerGuess(String PlayerGuess1) {
int PlayerGuess2 = Integer.parseInt(PlayerGuess1);
PlayerGuess = PlayerGuess2;
if(PlayerGuess2 >= 8) {
System.out.println("enter number smaller than 8 ");
}
}
现在,当您输入的数字大于7
时,系统会再次提示您。
答案 2 :(得分:-2)
你的错误:如果用户的猜测大于7,game.setPlayerGuess
没有设置数字becoz有一个&#34; if&#34;在方法内部。然后,你拨打game.getPlayerGuess()
,你基本上会得到之前输入的有效号码。
如上所述 - 无论如何,这是一个糟糕的设计!