这是我的代码,该程序是一个简单的猜数字游戏,其中计算机选择一个介于1和x之间的随机数(在程序内设置),用户将使用计算机的反馈来猜测该数字,说明是否每个猜测都高于或低于秘密数字。
代码由4种方法组成,这是一种主要方法,其中在显示播放指令后声明几个变量。然后设置while循环以开始新游戏。在while循环的3行中,调用了一种方法来开始玩新游戏,同时传递了扫描仪/控制台和一个名为guess的整数(每次调用此方法时设置为0)。
这个整数,每当用户进行一次猜测时,就会增加1,应该在游戏方法的结尾处返回,但是我似乎无法弄清楚为什么不返回。需要将其返回,以便可以将其传递给results方法,以计算如果用户决定不再玩游戏将显示的统计信息。
任何帮助将不胜感激...
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;
Game(guesses,console); //required
if (guesses > maxguesses) {
guesses = maxguesses;
}
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);
}
}
答案 0 :(得分:2)
这里发生了很多事情,并且所有这些都是相互依存的。
Game
返回的值的结果。Scanner
会遇到问题。让我们从低落的果实开始。 Game
返回一个int
,但未在任何地方分配。理想情况下,应将其分配给guesses
的值。
guesses = Game(guesses,console); //required
...除了在以下情况下传递guesses
并没有实际意义:
我们主要对其进行重新分配(不用担心,由于Java是pass by value,因此Game
方法将拥有自己的猜测副本)
您将其明确分配给Game
中的0 无论如何
因此,您想将其作为方法的参数删除。
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();
This is a surprisingly common Scanner
problem,并且很容易解决。
userNewGame = console.next();
console.nextLine();
// and inside of Game()
guess = console.nextInt();
console.nextLine();
或者,您可以改用nextLine
而不处理next()
,因为它们都返回String
。区别在于next()
不使用Return / Enter生成的换行符,而nextLine()
这样做。
答案 1 :(得分:1)
您应该使用返回的值更新变量,而不是仅调用Guesses方法,如下所示:
guesses = Game(guesses,console); //required
如果要让最大猜测者(在所有游戏中)保持最大猜想,那么应该在调用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