2 回答

TA貢獻1890條經(jīng)驗 獲得超9個贊
這里發(fā)生了一些事情,而且所有這些都建立在彼此的基礎上。
您沒有捕獲從Game.
您對變量做了一些非常奇怪的事情,這降低了可讀性。
Scanner當您循環(huán)執(zhí)行此程序時,您將遇到問題。
讓我們從唾手可得的果實開始。 Game返回一個int,但它沒有被分配到任何地方。理想情況下,它應該分配給 的值guesses。
guesses = Game(guesses,console); //required
...除了在以下情況下傳入并沒有真正意義guesses:
我們正在main 中重新分配它(別擔心,該Game方法無論如何都會有自己的猜測副本,因為 Java 是按值傳遞的)
無論如何,您明確地將其分配給 0Game
因此,您希望將其作為方法的參數(shù)刪除。
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()不消耗通過返回所產(chǎn)生的換行字符/確認,并且nextLine() 確實。

TA貢獻1809條經(jīng)驗 獲得超8個贊
您應該使用返回值來更新變量,而不是僅僅調(diào)用 Guesses 方法,如下所示:
guesses = Game(guesses,console); //required
如果您希望 maxguesses 保持最大數(shù)量的猜測(在所有游戲中),那么您應該在調(diào)用 Game 方法之后更新您的邏輯,如下所示,
if (guesses > maxguesses) {
maxguesses = guesses; // not guesses = maxguesses
}
這是我發(fā)布的整個代碼,我認為它運行良好??纯催@個:
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
添加回答
舉報