java - pig 游戏 : Why isn't the code working or compiling, 我做错了什么?

标签 java

简介

再进行一次循环练习。这个程序将比前一个程序更加复杂。

对于此程序,您将创建骰子游戏 Pig 的模拟。

游戏的目标是让玩家先于对手获得 100 分,更多细节将在任务中讨论。

在这种情况下,对手将是计算机。

任务1

此游戏需要两个 6 面骰子。您需要使用随机数生成器来模拟这些骰子。

两个骰子(随机数生成器)必须:

产生 1-6 之间的值 模具一和模具二分别有 140L 和 340L 的种子(用于测试目的) 任务2

现在您已经有了骰子,我们可以回顾一下您将要遵守的游戏规则。

规则:

每一回合,玩家将轮流掷两个骰子。 如果两个骰子上都没有出现 1,则这些值将添加到玩家的总数中。然后他们可以选择再次掷骰(选择 0)或将回合交给其他玩家(选择 1)。 如果其中一个骰子上出现 1,则该玩家在整个回合的总分中不会获得任何分数,并且轮到另一位玩家掷骰子(之前回合中获得的分数仍将出现在其总分中)。 如果玩家掷出的点数均为 1,则该玩家的回合结束,其总数将重置为 0。 您将需要另一个随机数生成器来确定计算机(0)还是玩家(1)先进行。这也将用于模拟计算机选择是再次滚动还是跳过翻转。该发电机的种子容量为 140L。

假设用户输入有效。

输出应以“Welcome of the Game of Pigs”语句开头

以下所有用户提示应类似于:

轮到你了(当前积分:0) 您掷出 3 和 2,本回合获得的积分:5 按 0 再次滚动或按 1 开始计算机转动 所有计算机提示应如下所示:

计算机回合(当前点数:0) 电脑掷出 1 和 4,没有得分,轮到你了 他们宣布哪个玩家的回合和当前分数。

然后是由哪个玩家掷出的数字,然后是

如果获得积分,则显示该回合获得的总积分(参见用户提示第二行) 如果掷出一个,则宣布没有获得分数并轮到下一个玩家(参见计算机提示第二行) 如果两者都是 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

最佳答案

if (playerTotal > ComputerTotal) 行之前有太多的大括号。去掉一个,结构应该没问题。问题是,由于大括号太多,您会在最后一个 if 语句之前关闭该方法,因此该语句基本上位于方法之外,这在 Java 中是不允许的。如果您使用 IDE,它应该会向您显示这一点。

关于java - pig 游戏 : Why isn't the code working or compiling, 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47235555/

相关文章:

java - 为什么 println(array) 有奇怪的输出? ("[Ljava.lang.String;@3e25a5")

java - 双链表快速排序

java - 为什么小于 int 的数据类型中的两个数字相加会转换为 int?

java - 计算java中一个HashMap的内存占用

java - 有没有办法用相同的方法创建不同类型的对象?

java - 当我使用 : int cannot be converted to String, this 时出现错误 : timer = new Timer(1000,);

仅保存日期的 Java 数据类型

java - 将数据从 Java 进程传递到 python 脚本

java - HTTP 状态 500 - PHP FastCGI 实例失败

java - 使用多线程对文件进行排序