第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何正確傳遞變量?

如何正確傳遞變量?

拉風的咖菲貓 2021-12-10 14:57:54
這是我的代碼,這個程序是一個簡單的猜數(shù)游戲,其中計算機在 1 和 x 之間選擇一個隨機數(shù)(在程序中設置),用戶將使用計算機的反饋來猜測數(shù)字,說明每個猜測是否正確高于或低于秘密數(shù)字。代碼由4個方法組成,一個main方法,在顯示播放指令后聲明了幾個變量。然后設置一個 while 循環(huán)來開始一個新游戲。在 while 循環(huán)中的 3 行中,調(diào)用了一個方法來開始玩新游戲,同時傳遞掃描儀/控制臺和一個稱為 guesses 的整數(shù)(每次調(diào)用此方法時都設置為 0)。這個整數(shù),猜測,每次用戶進行猜測時都會增加一,應該在游戲方法結束時返回,但我似乎無法弄清楚為什么它沒有被返回。它需要返回,以便可以將其傳遞給 results 方法來計算如果用戶決定不再玩游戲將顯示的統(tǒng)計信息。任何幫助,將不勝感激...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條經(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() 確實。


查看完整回答
反對 回復 2021-12-10
?
海綿寶寶撒

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


查看完整回答
反對 回復 2021-12-10
  • 2 回答
  • 0 關注
  • 113 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號