2017-11-11 54 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吗? –