为了账号安全,请及时绑定邮箱和手机立即绑定

如何正确传递变量?

如何正确传递变量?

拉风的咖菲猫 2021-12-10 14:57:54
这是我的代码,这个程序是一个简单的猜数游戏,其中计算机在 1 和 x 之间选择一个随机数(在程序中设置),用户将使用计算机的反馈来猜测数字,说明每个猜测是否正确高于或低于秘密数字。代码由4个方法组成,一个main方法,在显示播放指令后声明了几个变量。然后设置一个 while 循环来开始一个新游戏。在 while 循环中的 3 行中,调用了一个方法来开始玩新游戏,同时传递扫描仪/控制台和一个称为 guesses 的整数(每次调用此方法时都设置为 0)。这个整数,猜测,每次用户进行猜测时都会增加一,应该在游戏方法结束时返回,但我似乎无法弄清楚为什么它没有被返回。它需要返回,以便可以将其传递给 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   } 
查看完整描述

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() 确实。


查看完整回答
反对 回复 2021-12-10
?
海绵宝宝撒

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


查看完整回答
反对 回复 2021-12-10
  • 2 回答
  • 0 关注
  • 98 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信