簡體   English   中英

*更新*現在我的程序可以編譯但不能運行?

[英]*UPDATE* now my program compiles but doesn't run?

我正在編寫一個程序,提示用戶在計算機上玩剪刀石頭布。 我在從main方法調用int rpsls方法時遇到麻煩。 我不確定如何從main方法調用它,因為我需要將main方法中用戶輸入的值輸入到rpsls方法中。 以下是到目前為止的內容,是的,它充滿了錯誤,但是我主要關心的是調用該方法。

更新 -我添加了更多代碼,現在程序將編譯。 但是,當我嘗試運行它時,它只是凍結了。 誰能幫助找到使程序運行的解決方案?

import java.util.Scanner; 
public class Rpsls
{  



   public static void main (String [] args) {
      int wins = 0;
      int ties = 0;
      int losses = 0;
      int retVal = 0;
      int output = rpsls(retVal);
      //while loop 
      Scanner input = new Scanner (System.in);
      int x = input.nextInt();
         while (x > 0 && x <= 5) {
            //input gesture 

            System.out.println(" ROCK PAPER SCISSORS LIZARD SPOCK");
            System.out.println(" (1)   (2)     (3)     (4)    (5)");
            System.out.print("Enter your choice, or 0 to end: ");

            //call rpsls for inputted gesture
            int gesture = input.nextInt();
            rpsls(gesture);
            //returned values: loss, win, or tie
            if (output == -1 ) {
               losses++;
            }
               else if (output == 0) {
                  ties++;
               }
            else {
               wins++;
            }
            //count wins and losses 
            //end while loop 
            //print sum of wins and losses. 
         }
         System.out.println("You won " + wins + " games, lost " + losses + " games, and tied " + ties + " games.");

   }

   public static int rpsls(int gesture) {
      //generate random gesture for computer 
      int attack = (int)(Math.random()*5);
      //decide who won 
           int wins = 0; int losses = 0; int ties = 0;
           int retVal = 0;     

            if (gesture == 1 && attack == 1) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose ROCK.");
               System.out.println("It's a tie!");
               retVal = 0;
            }
            if (gesture == 1 && attack == 2) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose PAPER.");
               System.out.println("PAPER covers ROCK.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 1 && attack == 3) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose SCISSORS.");
               System.out.println("ROCK crushes SCISSORS.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 1 && attack == 4) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose LIZARD.");
               System.out.println("ROCK crushes LIZARD.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 1 && attack == 5) {
               System.out.println("You chose ROCK.");
               System.out.println("Computer chose SPOCK.");
               System.out.println("SPOCK vaporizes ROCK.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 2 && attack == 1) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose ROCK.");
               System.out.println("PAPER covers ROCK.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 2 && attack == 2) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose PAPER.");
               System.out.println("It's a tie!");
               retVal = 0;
            }
            if (gesture == 2 && attack == 3) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose SCISSORS.");
               System.out.println("SCISSORS cut PAPER.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 2 && attack == 4) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose LIZARD.");
               System.out.println("LIZARD eats PAPER.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 2 && attack == 5) {
               System.out.println("You chose PAPER.");
               System.out.println("Computer chose SPOCK.");
               System.out.println("PAPER disproves SPOCK.");
               System.out.println("You win!");
               retVal = 1;
            }
            if (gesture == 3 && attack == 1) {
               System.out.println("You chose SCISSORS.");
               System.out.println("Computer chose ROCK.");
               System.out.println("ROCK crushes SCISSORS.");
               System.out.println("Computer wins!");
               retVal = -1;
            }
            if (gesture == 3 && attack == 2) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose PAPER.");
              System.out.println("SCISSORS cut PAPER.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 3 && attack == 3) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose SCISSORS.");
              System.out.println("It's a tie!");
              retVal = 0;
            }
            if (gesture == 3 && attack == 4) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose LIZARD.");
              System.out.println("SCISSORS decapitate LIZARD.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 3 && attack == 5) {
              System.out.println("You chose SCISSORS.");
              System.out.println("Computer chose SPOCK.");
              System.out.println("SPOCK smashes SCISSORS.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 4 && attack == 1) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose ROCK.");
              System.out.println("ROCK crushes LIZARD.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 4 && attack == 2) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose PAPER.");
              System.out.println("LIZARD eats PAPER.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 4 && attack == 3) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose SCISSORS.");
              System.out.println("SCISSORS decapitate LIZARD.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 4 && attack == 4) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose LIZARD.");
              System.out.println("It's a tie!");
              retVal = 0;
            }
            if (gesture == 4 && attack == 5) {
              System.out.println("You chose LIZARD.");
              System.out.println("Computer chose SPOCK.");
              System.out.println("LIZARD poisons SPOCK.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 5 && attack == 1) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose ROCK.");
              System.out.println("SPOCK vaporizes ROCK.");
              System.out.println("You win!");
              retVal = 1;
            } 
            if (gesture == 5 && attack == 2) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose PAPER.");
              System.out.println("PAPER disproves SPOCK.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 5 && attack == 3) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose SCISSORS.");
              System.out.println("SPOCK smashes SCISSORS.");
              System.out.println("You win!");
              retVal = 1;
            }
            if (gesture == 5 && attack == 4) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose LIZARD.");
              System.out.println("LIZARD poisons SPOCK.");
              System.out.println("Computer wins!");
              retVal = -1;
            }
            if (gesture == 5 && attack == 5) {
              System.out.println("You chose SPOCK.");
              System.out.println("Computer chose SPOCK.");
              System.out.println("It's a tie!");
              retVal = 0;
            } 
            return retVal;
   }

}

這行:

int rpsls = gesture(x);

...創建一個int稱為varaible rpslsmain函數,調用調用方法gesture ,並通過x進去。

您可能是說:

int gesture = input.nextInt();
rpsls(gesture);

...這會在您的Scanner上調用nextInt來從用戶那里獲取一個數字,將其記住為gesture ,然后調用您現有的rpsls方法,並傳遞gesture

  1. 您正在調用gesture(int),並且可能正在嘗試調用int rpsls(int pose) int retVal = rpsls(gesture) ;
  2. 一些變量在影響其生命周期的代碼塊中初始化和聲明,例如, input在while塊內部,因此它將在外部不起作用。
  3. int rpsls(int gesture)設計不當:計算機應為石頭/紙張/剪刀選擇一個隨機值,然后將其與用戶進行比較;

您的代碼無效,您至少需要:

  • 移動int x = input.nextInt(); while (x > 0 && x <= 5)語句之后,如果沒有此語句,它將不會將您的輸入用於游戲,
  • 在首次使用Scanner聲明之前將其移動,因為它不會編譯,
  • 在“手勢”上更改rpsls方法中的所有“ x”,rpsls方法無法訪問main方法中的“ x”變量;
  • 初始化“ retVal”變量(給它一個值),例如0,以避免編譯錯誤;
  • 更改int rpsls = gesture(x); 例如int rpsls = rpsls(x); 這樣,它將繼續您的輸入

我認為這些更改之后效果很好。

編輯:您應該刪除int output = rpsls(retVal); 和retVal聲明從主,因為我認為這沒用,並添加int output = rpsls(x); x = input.nextInt(); 然后它可以正常工作。 還替換int x = input.nextInt(); int gesture = input.nextInt();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM