gpt4 book ai didi

java - 如何正确传递变量?

转载 作者:行者123 更新时间:2023-12-01 19:46:36 25 4
gpt4 key购买 nike

这是我的代码,这个程序是一个简单的猜数字游戏,其中计算机在 1 和 x 之间选择一个随机数(在程序中设置),用户将使用计算机的反馈来猜测该数字,说明是否每个猜测都高于或低于 secret 数字。

该代码由4个方法组成,一个主方法,在显示播放指令后声明了几个变量。然后设置一个 while 循环来开始新游戏。在 while 循环中的第 3 行中,调用一个方法来开始玩新游戏,同时传递扫描仪/控制台和一个名为guesses 的整数(每次调用此方法时将其设置为 0)。

这个整数,猜测,每次用户进行猜测时都会增加一,并且应该在游戏方法结束时返回,但我似乎无法弄清楚为什么它没有被返回。需要返回它,以便可以将其传递到结果方法来计算用户决定不再玩时将显示的统计信息。

如有任何帮助,我们将不胜感激...

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);
}
}

最佳答案

这里发生了一些事情,而且所有这些事情都是相互构建的。

  • 您没有捕获从 Game 返回的值的结果.
  • 您对变量做了一些非常奇怪的事情,这降低了可读性。
  • 您的 Scanner 将会遇到问题当您循环执行此程序时,会一直向下。

让我们从容易实现的目标开始。 Game返回 int ,但它没有被分配到任何地方。理想情况下,应将其分配给值 guesses .

guesses = Game(guesses,console); //required

...除了传递 guesses 并没有真正意义何时:

  • 我们在 main 中重新分配它(不用担心,Game 方法无论如何都会有自己的猜测副本,因为 Java 是 pass by value )

  • 您将其显式分配给 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() 确实

关于java - 如何正确传递变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53017610/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com