2017-11-11 13 views
1

引言豬的遊戲:爲什麼代碼不工作或編譯,我做錯了什麼?

一個循環的做法。這個計劃將會涉及到前一個。

對於這個程序,你將創建一個模擬骰子游戲豬。

該遊戲的目標是讓玩家在對手之前獲得100分,其他細節將在任務中討論。

在這種情況下,對手將是電腦。

任務1

這個遊戲需要兩個6面骰子。你需要用隨機數發生器模擬這些骰子。

兩個骰子(隨機數生成器)必須:1-6 之間

農產品值具有140L和340L的種子,分別爲模具和一個模2(用於測試目的) 任務2

現在你已經有了骰子,我們可以重複你將要玩的規則。

規則:

對於每一回合,玩家將輪流滾動兩個骰子。 如果一個骰子沒有出現在骰子上,這些值就會加到玩家的總數上。然後他們可以選擇再次擲出(選擇0)或將轉牌傳給其他玩家(選擇1)。 如果某個骰子出現1,則該玩家在整個回合中獲得的總積分不得增加,併成爲其他玩家的回合(前一回合獲得的積分將依然存在於其總數中)。 如果玩家擲出兩個1s,則玩家的回合結束並且他們的總數被重置爲0. 您需要一個隨機數字生成器來確定計算機(0)或玩家(1)是否會先玩。這也將被用來模擬計算機的選擇,以再次滾動或通過翻轉。這臺發電機的種子將是140L。

假定來自用戶的有效輸入。

輸出應該

以下所有用戶提示應該類似於語句「豬的博弈歡迎」開頭:

輪到你了(目前積分:0) 您推出3和2,分獲得本回合:5 按0再次或1卷啓動電腦的回合 和所有計算機的提示應該是這樣的:

電腦的回合(當前點:0) 電腦推出1和4,沒有點賺取的輪到你 他們宣佈哪個玩家的回合和當前分數。這是由什麼播放器,然後

如果分榮獲推出

然後是數字,顯示贏得了該輪(看到用戶的提示2號線) 如果一個被軋總積分,宣佈獲得無分,接下來的玩家回合(見電腦提示2號線) 如果同時的,利用顯示消息「取其播放器/捲起兩個1 /,點重置和/對手的/轉」

import java.util.Scanner; 
import java.util.Random; 

public class GameOfPigs { 
    public static void main(String[] args) { 
     Random die1 = new Random(140L); 
     Random die2 = new Random(340L); 
     Random compDecision = new Random(140L); 
     Scanner scnr = new Scanner(System.in); 

     int computerTotal = 0; 
     int playerTotal = 0; 
     boolean playerTurn = true; 

     // Decides who goes first... 
     if ((compDecision.nextInt(2)) == 0) { 
      playersTurn = false; 
     } 

     System.out.println("Welcome of the Game of Pigs"); 

     // Main game loop 
     while (computerTotal < 100 && playerTotal < 100) { 
      System.out.println(); 
      int currentPlayerPoints = 0; 

      // Player's loop 
      while (playersTurn) { 
       System.out.println("Your turn (current points: " + playerTotal + ")"); 
       int roll1 = die1.nextInt(6) + 1; 
       int roll2 = die2.nextInt(6) + 1; 

       // First Rule...Not the same as the example in class!!! 
       // Adjust accordingly!!!! Multiple ways to do this!!!! 
       if (roll1 == 1 && roll2 == 1) { 
        playerTotal = 0; 
        playersTurn = false; 
        break; 
       } 

       // Second Rule 
       else if (roll1 == 1 || roll2 == 1) { 
        playerTotal = playerTotal; 
        playersTurn = false; 
        break; 
       } 

       // Third Rule 
       else { 
        playerTotal += currentPlayerPoints; 
        int choice = scnr.nextInt(); 
        if (choice == 1) { 
         playerTotal += currentPlayerPoints; 
         playersTurn = false; 
         break; 
        } 
       } 
      } 
      if (playerTotal >= 100) { 
       break; 
      } 

      // 
      int currentCompPoints = 0; 

      // Computer's loop 
      while (!playersTurn) { 
       System.out.println("Computer's turn (current points: " + computerTotal + ")"); 
       int roll1 = die1.nextInt(6)+1; 
       int roll2 = die2.nextInt(6)+1; 

       if (roll1 == 1 && roll2 == 1) { 
        computerTotal = 0; 
       } 
       else if (roll1 == 1 || roll2 == 1) { 
        computerTotal = computerTotal; 
       } 
       else { 
        int choice = compDecision.nextInt(2); 
        computerTotal += currentPlayerPoints; 
        } 
       } 
      }   
     } 
     if (playerTotal > computerTotal) { 
      System.out.println("Congratulations! You won!"); 
     } 
     else { 
      System.out.println("Too Bad, the computer won."); 
     } 
    } 
} 

GameOfPigs.java:86: error: illegal start of type 
     if (playerTotal > computerTotal) { 
     ^
GameOfPigs.java:86: error: <identifier> expected 
     if (playerTotal > computerTotal) { 
        ^
GameOfPigs.java:86: error: ';' expected 
     if (playerTotal > computerTotal) { 
         ^
GameOfPigs.java:86: error: illegal start of type 
     if (playerTotal > computerTotal) { 
            ^
GameOfPigs.java:86: error: <identifier> expected 
     if (playerTotal > computerTotal) { 
             ^
GameOfPigs.java:86: error: ';' expected 
     if (playerTotal > computerTotal) { 
             ^
GameOfPigs.java:87: error: illegal start of type 
      System.out.println("Congratulations! You won!"); 
       ^
GameOfPigs.java:87: error: ';' expected 
      System.out.println("Congratulations! You won!"); 
        ^
GameOfPigs.java:87: error: invalid method declaration; return type required 
      System.out.println("Congratulations! You won!"); 
        ^
GameOfPigs.java:87: error: illegal start of type 
      System.out.println("Congratulations! You won!"); 
          ^
GameOfPigs.java:89: error: class, interface, or enum expected 
     else { 
     ^
GameOfPigs.java:91: error: class, interface, or enum expected 
     } 
     ^
12 errors 

謝謝!但現在它說:

GameOfPigs.java:18: error: cannot find symbol 
      playersTurn = false; 
      ^
    symbol: variable playersTurn 
    location: class GameOfPigs 
GameOfPigs.java:29: error: cannot find symbol 
      while (playersTurn) { 
       ^
    symbol: variable playersTurn 
    location: class GameOfPigs 
GameOfPigs.java:29: error: illegal start of type 
      while (playersTurn) { 
       ^
GameOfPigs.java:38: error: cannot find symbol 
        playersTurn = false; 
        ^
    symbol: variable playersTurn 
    location: class GameOfPigs 
GameOfPigs.java:45: error: cannot find symbol 
        playersTurn = false; 
        ^
    symbol: variable playersTurn 
    location: class GameOfPigs 
GameOfPigs.java:55: error: cannot find symbol 
         playersTurn = false; 
         ^
    symbol: variable playersTurn 
    location: class GameOfPigs 
GameOfPigs.java:68: error: cannot find symbol 
      while (!playersTurn) { 
        ^
    symbol: variable playersTurn 
    location: class GameOfPigs 
7 errors 
+0

「這個程序會比以前更復雜..」,請爲我做作業嗎?看起來你並沒有付出太多努力,甚至試圖自己先解決問題。即你在哪裏開始和失敗? –

+0

現在我可以添加代碼。對不起,以前,它不會讓我。 – Muhammad

+0

我確實試圖首先自己解決問題。現在我能夠發佈我的代碼,可以看到我開始和失敗的位置。 – Muhammad

回答

0

if (playerTotal > computerTotal)之前有一個花括號太多。刪除一個,結構應該沒問題。問題在於,如果你在最後一條if語句之前關閉了方法,那麼該語句基本上不在Java中允許的方法之外。如果您使用IDE,它應該向您顯示。

+0

謝謝!但現在它說: GameOfPigs.java:18:錯誤:找不到符號 playersTurn = false; ^ 符號:可變playersTurn 位置:類GameOfPigs GameOfPigs.java:29:錯誤:找不到符號 而(playersTurn){ ^ 符號:可變playersTurn 位置:類GameOfPigs GameOfPigs.java:29:錯誤: 類型而非法啓動(playersTurn){ ^ GameOfPigs.java:38:錯誤:找不到符號 playersTurn = FALSE; ^ – Muhammad

+0

符號:變量球員轉身 位置:class GameOfPigs GameOfPigs.java:45:錯誤:找不到符號 playersTurn = false; ^ 符號:變量球員轉身 位置:class GameOfPigs GameOfPigs.java:55:錯誤:找不到符號 playersTurn = false; ^ 符號:變量球員轉身 位置:class GameOfPigs GameOfPigs.java:68:錯誤:無法找到符號 while(!playersTurn){ ^ 符號:變量playersTurn 位置:類GameOfPigs 7錯誤 – Muhammad

0

*請注意,此答案只會刪除先前在您的代碼中指定的錯誤,並不一定作爲您問題的最終解決方案。

import java.util.Scanner; 
import java.util.Random; 

public class GameOfPigs { 



    public static void main(String[] args) { 
     Random die1 = new Random(140L); 
     Random die2 = new Random(340L); 
     Random compDecision = new Random(140L); 

     Scanner scnr = new Scanner(System.in); 

     int computerTotal = 0; 
     int playerTotal = 0; 
     boolean playersTurn = false; 

     // Decides who goes first... 
     if ((compDecision.nextInt(2)) == 0) { 
      playersTurn = false; 
     } 

     System.out.println("Welcome of the Game of Pigs"); 

     // Main game loop 
     while (computerTotal < 100 && playerTotal < 100) { 
      System.out.println(); 
      int currentPlayerPoints = 0; 

      // Player's loop 
      while (playersTurn) { 

       int roll1 = die1.nextInt(6) + 1; 
       int roll2 = die2.nextInt(6) + 1; 
       System.out.println("Your turn (current points: " + playerTotal + ")"); 
       // First Rule...Not the same as the example in class!!! 
       // Adjust accordingly!!!! Multiple ways to do this!!!! 
       if (roll1 == 1 && roll2 == 1) { 
        playerTotal = 0; 
        playersTurn = false; 
        break; 
       } 

       // Second Rule 
       else if (roll1 == 1 || roll2 == 1) { 
        playerTotal = playerTotal; 
        playersTurn = false; 
        break; 
       } 

       // Third Rule 
       else { 
        playerTotal += currentPlayerPoints; 
        int choice = scnr.nextInt(); 
        if (choice == 1) { 
         playerTotal += currentPlayerPoints; 
         playersTurn = false; 
         break; 
        } 
       } 
      } 
      if (playerTotal >= 100) { 
       break; 
      }   // 
      int currentCompPoints = 0; 

      // Computer's loop 
      while (!playersTurn) { 
       System.out.println("Computer's turn (current points: " + computerTotal + ")"); 
       int roll1 = die1.nextInt(6)+1; 
       int roll2 = die2.nextInt(6)+1; 

       if (roll1 == 1 && roll2 == 1) { 
        computerTotal = 0; 
       } 
       else if (roll1 == 1 || roll2 == 1) { 
        computerTotal = computerTotal; 
       } 
       else { 
        int choice = compDecision.nextInt(2); 
        computerTotal += currentPlayerPoints; 
        } 
       } 


     if (playerTotal > computerTotal) { 
      System.out.println("Congratulations! You won!"); 
     } 
     else { 
      System.out.println("Too Bad, the computer won."); 
     } 
    } 
} 
} 
+0

謝謝,但現在它說: 計劃產生太大的輸出。 輸出限制爲50000個字符。 檢查程序是否有任何未終止的循環產生輸出。 – Muhammad

+0

@Muhammad,您正在爲您的項目使用IDE嗎? –

相關問題