如何正确传递变量?

这是我的代码,这个程序是一个简单的猜数游戏,其中计算机在 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

   }

 

拉风的咖菲猫
浏览 99回答 2
2回答

当年话下

这里发生了一些事情,而且所有这些都建立在彼此的基础上。您没有捕获从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) {&nbsp; &nbsp; Random rand = new Random();&nbsp; &nbsp; 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.&nbsp; &nbsp; int number = rand.nextInt(range) + 1;&nbsp; &nbsp; int guesses = 0;&nbsp; &nbsp; int guess = -1;&nbsp; &nbsp; System.out.println("I'm thinking of a number...");&nbsp; &nbsp; while (guess != number) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Your guess? ");&nbsp; &nbsp; &nbsp; &nbsp; guess = console.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; guesses = guesses + 1;&nbsp; &nbsp; &nbsp; &nbsp; if (guess < number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("higher");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (guess > number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("lower");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (guess == number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You got it right in " + guesses + " guesses");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; 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() 确实。

海绵宝宝撒

您应该使用返回值来更新变量,而不是仅仅调用 Guesses 方法,如下所示:guesses = Game(guesses,console); //required如果您希望 maxguesses 保持最大数量的猜测(在所有游戏中),那么您应该在调用 Game 方法之后更新您的逻辑,如下所示,&nbsp; &nbsp; &nbsp;if (guesses > maxguesses) {&nbsp; &nbsp; &nbsp; &nbsp; maxguesses = guesses; // not guesses = maxguesses&nbsp; &nbsp; &nbsp;}这是我发布的整个代码,我认为它运行良好。看看这个:&nbsp; &nbsp; import java.util.*;public class Guess {&nbsp; &nbsp;public static void main(String[] Args) {&nbsp; &nbsp; &nbsp; Scanner console = new Scanner(System.in);&nbsp; &nbsp; &nbsp; Introduction(); //required&nbsp; &nbsp; &nbsp; int games = 0;&nbsp; &nbsp; &nbsp; int newGame = 1;&nbsp; &nbsp; &nbsp; int guesses = 0;&nbsp; &nbsp; &nbsp; int maxguesses = 0;&nbsp; &nbsp; &nbsp; int totalguesses = 0;&nbsp; &nbsp; &nbsp; while (newGame == 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;String userNewGame = "";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;games = games + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;guesses = Game(guesses,console); //required&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (guesses > maxguesses) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; maxguesses = guesses;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;totalguesses = totalguesses + guesses;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print("Do you want to play again? ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;userNewGame = console.next();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;char first = userNewGame.charAt(0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if ( first == 'Y' || first == 'y') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newGame = 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;else if ( first == 'N' || first == 'n') {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; newGame = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; Results(games,totalguesses,maxguesses); //required&nbsp; &nbsp;}&nbsp; &nbsp;public static void Introduction() {&nbsp; &nbsp; &nbsp; System.out.println("This program allows you to play a guessing game.");&nbsp; &nbsp; &nbsp; System.out.println("I will think of a number between 1 and 100");&nbsp; &nbsp; &nbsp; System.out.println("and will allow you to guess until you get it.");&nbsp; &nbsp; &nbsp; System.out.println("For each guess, I will tell you whether the");&nbsp; &nbsp; &nbsp; System.out.println("right answer is higher or lower than your guess.");&nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp;}&nbsp; &nbsp;public static int Game(int guesses, Scanner console) {&nbsp; &nbsp; &nbsp; Random rand = new Random();&nbsp; &nbsp; &nbsp; 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.&nbsp; &nbsp; &nbsp; int number = rand.nextInt(range) + 1;&nbsp; &nbsp; &nbsp; guesses = 0;&nbsp; &nbsp; &nbsp; int guess = -1;&nbsp; &nbsp; &nbsp; System.out.println("I'm thinking of a number...");&nbsp; &nbsp; &nbsp; while (guess != number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.print("Your guess? ");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;guess = console.nextInt();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;guesses = guesses + 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (guess < number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("higher");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (guess > number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("lower");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if (guess == number) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You got it right in " + guesses + " guesses");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return guesses;&nbsp; &nbsp;}&nbsp; &nbsp;public static void Results(int games,int totalguesses,int maxguesses) {&nbsp; &nbsp; &nbsp; System.out.println("Overall results:");&nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; total games&nbsp; &nbsp;= " + games);&nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; total guesses = " + totalguesses);&nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; guesses/game&nbsp; = " + totalguesses / games);&nbsp; &nbsp; &nbsp; System.out.println("&nbsp; &nbsp; max guesses&nbsp; &nbsp;= " + maxguesses);&nbsp; &nbsp;}}示例输出:&nbsp; &nbsp; This program allows you to play a guessing game.I will think of a number between 1 and 100and will allow you to guess until you get it.For each guess, I will tell you whether theright answer is higher or lower than your guess.I'm thinking of a number...Your guess? 10higherYour guess? 50lowerYour guess? 40lowerYour guess? 30lowerYour guess? 20lowerYour guess? 15You got it right in 6 guessesDo you want to play again? yI'm thinking of a number...Your guess? 50higherYour guess? 80higherYour guess? 90lowerYour guess? 85You got it right in 4 guessesDo you want to play again? nOverall results:&nbsp; &nbsp; total games&nbsp; &nbsp;= 2&nbsp; &nbsp; total guesses = 10&nbsp; &nbsp; guesses/game&nbsp; = 5&nbsp; &nbsp; max guesses&nbsp; &nbsp;= 6
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java