2 回答
TA贡献1890条经验 获得超9个赞
这里发生了一些事情,而且所有这些都建立在彼此的基础上。
您没有捕获从Game.
您对变量做了一些非常奇怪的事情,这降低了可读性。
Scanner当您循环执行此程序时,您将遇到问题。
让我们从唾手可得的果实开始。 Game返回一个int,但它没有被分配到任何地方。理想情况下,它应该分配给 的值guesses。
guesses = Game(guesses,console); //required
...除了在以下情况下传入并没有真正意义guesses:
我们正在main 中重新分配它(别担心,该Game方法无论如何都会有自己的猜测副本,因为 Java 是按值传递的)
无论如何,您明确地将其分配给 0Game
因此,您希望将其作为方法的参数删除。
guesses = Game(console);
在 中Game,您可以定义自己的guesses变量。
public static int Game(Scanner console) {
Random rand = new Random();
int range = 100; //Change this value to set the range the computer will require to guess ie. 100 is 1 to 100 inclusive, 5 is 1 to 5 inclusive, etc.
int number = rand.nextInt(range) + 1;
int guesses = 0;
int guess = -1;
System.out.println("I'm thinking of a number...");
while (guess != number) {
System.out.print("Your guess? ");
guess = console.nextInt();
guesses = guesses + 1;
if (guess < number) {
System.out.println("higher");
}
if (guess > number) {
System.out.println("lower");
}
if (guess == number) {
System.out.println("You got it right in " + guesses + " guesses");
System.out.println();
}
}
return guesses;
}
我能看到的最后一个明显问题是您正在使用的问题next(),但您并没有完全清除Scanner.
userNewGame = console.next();
// and inside of Game()
guess = console.nextInt();
这是一个令人惊讶的常见Scanner问题,而且很容易解决。
userNewGame = console.next();
console.nextLine();
// and inside of Game()
guess = console.nextInt();
console.nextLine();
或者,您可以使用nextLine而不是处理,next()因为它们都返回String. 不同之处在于next()不消耗通过返回所产生的换行字符/确认,并且nextLine() 确实。
TA贡献1809条经验 获得超8个赞
您应该使用返回值来更新变量,而不是仅仅调用 Guesses 方法,如下所示:
guesses = Game(guesses,console); //required
如果您希望 maxguesses 保持最大数量的猜测(在所有游戏中),那么您应该在调用 Game 方法之后更新您的逻辑,如下所示,
if (guesses > maxguesses) {
maxguesses = guesses; // not guesses = maxguesses
}
这是我发布的整个代码,我认为它运行良好。看看这个:
import java.util.*;
public class Guess {
public static void main(String[] Args) {
Scanner console = new Scanner(System.in);
Introduction(); //required
int games = 0;
int newGame = 1;
int guesses = 0;
int maxguesses = 0;
int totalguesses = 0;
while (newGame == 1) {
String userNewGame = "";
games = games + 1;
guesses = Game(guesses,console); //required
if (guesses > maxguesses) {
maxguesses = guesses;
}
totalguesses = totalguesses + guesses;
System.out.print("Do you want to play again? ");
userNewGame = console.next();
System.out.println();
char first = userNewGame.charAt(0);
if ( first == 'Y' || first == 'y') {
newGame = 1;
}
else if ( first == 'N' || first == 'n') {
newGame = 0;
}
}
Results(games,totalguesses,maxguesses); //required
}
public static void Introduction() {
System.out.println("This program allows you to play a guessing game.");
System.out.println("I will think of a number between 1 and 100");
System.out.println("and will allow you to guess until you get it.");
System.out.println("For each guess, I will tell you whether the");
System.out.println("right answer is higher or lower than your guess.");
System.out.println();
}
public static int Game(int guesses, Scanner console) {
Random rand = new Random();
int range = 100; //Change this value to set the range the computer will require to guess ie. 100 is 1 to 100 inclusive, 5 is 1 to 5 inclusive, etc.
int number = rand.nextInt(range) + 1;
guesses = 0;
int guess = -1;
System.out.println("I'm thinking of a number...");
while (guess != number) {
System.out.print("Your guess? ");
guess = console.nextInt();
guesses = guesses + 1;
if (guess < number) {
System.out.println("higher");
}
if (guess > number) {
System.out.println("lower");
}
if (guess == number) {
System.out.println("You got it right in " + guesses + " guesses");
System.out.println();
}
}
return guesses;
}
public static void Results(int games,int totalguesses,int maxguesses) {
System.out.println("Overall results:");
System.out.println(" total games = " + games);
System.out.println(" total guesses = " + totalguesses);
System.out.println(" guesses/game = " + totalguesses / games);
System.out.println(" max guesses = " + maxguesses);
}
}
示例输出:
This program allows you to play a guessing game.
I will think of a number between 1 and 100
and will allow you to guess until you get it.
For each guess, I will tell you whether the
right answer is higher or lower than your guess.
I'm thinking of a number...
Your guess? 10
higher
Your guess? 50
lower
Your guess? 40
lower
Your guess? 30
lower
Your guess? 20
lower
Your guess? 15
You got it right in 6 guesses
Do you want to play again? y
I'm thinking of a number...
Your guess? 50
higher
Your guess? 80
higher
Your guess? 90
lower
Your guess? 85
You got it right in 4 guesses
Do you want to play again? n
Overall results:
total games = 2
total guesses = 10
guesses/game = 5
max guesses = 6
添加回答
举报