" Pig"出错游戏Java计划

时间:2015-09-09 02:16:01

标签: java

我正在尝试编写程序来玩游戏Pig和我两次收到此错误,但我不知道原因: "错误:找不到符号Scanner sc = new Scanner(System.in);

非常感谢任何帮助。我是编写代码的新手,所以请善待。 :)另外,忽略奇怪的间距...它在我的程序中正确完成但是我无法在此处正确地适应它。

这是我的计划:

class Die {

    private final int MAX = 6;
    private int faceValue;

    public Die() {
        faceValue = 1;
    }

    public int roll() {
        faceValue = (int) (Math.random() * MAX) + 1;
        return faceValue;
    }

    public void setFaceValue(int value) {
        if (value > 0 && value <= MAX) {
            faceValue = value;
        }
    }

    public int getFaceValue() {
        return faceValue;
    }

    public String toString() {
        String result = Integer.toString(faceValue);
        return result;
    }
}

class PairOfDice {

    private Die die1, die2;

    public PairOfDice() {
        die1 = new Die();
        die2 = new Die();
    }

    public int roll() {
        return die1.roll() + die2.roll();
    }

    public int getTotalFaceValue() {
        return die1.getFaceValue() + die2.getFaceValue();
    }

    public void setDie1FaceValue(int value) {
        die1.setFaceValue(value);
    }

    public void setDie2FaceValue(int value) {
        die2.setFaceValue(value);
    }

    public int getDie1FaceValue() {
        return die1.getFaceValue();
    }

    public int getDie2FaceValue() {
        return die2.getFaceValue();
    }

    public String toString() {
        return "Die 1: " + die1.getFaceValue()
                        + " Die 2: " + die2.getFaceValue();
    }
}

public class Pig {

    int cTotal = 0;
    int pTotal = 0;
    static boolean play = true;
    static int s = 1;
    static int Max = 100;

    PairOfDice User = new PairOfDice();
    PairOfDice Comp = new PairOfDice();
    Scanner sc = new Scanner(System.in);
    static Pig game = new Pig();

    public static void main(String[] args) {
        while (play) {
            System.out.println("Computer Roll :");
            game.Comp();
            System.out.println("\nUser Roll :");
            game.User();
        }
    }

    public void User() {
        int tot = 0;
        System.out.println("\nUser");
        if (pTotal <= 100) {
            System.out.println("[Pass=0,Roll=1]");
            s = sc.nextInt();
            if (s == 1) {
                while (tot < 20 && s == 1) {
                    User.roll();
                    System.out.println(User);

                    if (User.getDie1FaceValue() == 1
                                    || User.getDie2FaceValue() == 1) {
                        System.out.println("Moves to Computer");
                        game.Comp();
                        s = 0;
                    } else if (User.getDie1FaceValue() == 1
                                    && User.getDie2FaceValue() == 1) {
                        pTotal = 0;
                        System.out.println("Moves to Computer");
                        game.Comp();
                        s = 0;
                    } else {
                        tot = tot + User.getTotalFaceValue();

                        pTotal = pTotal + User.getTotalFaceValue();
                        System.out.println("Round Total :" + tot);
                        System.out.println("User Total :" + pTotal);
                        System.out.println("[Pass=0,Roll=1] :");
                        s = sc.nextInt();
                    }
                }
            } else {
                System.out.println("\nControl Moves to Computer");
                game.Comp();
            }
        } else {
            play = false;
        }
    }

    public void Comp() {
        System.out.println("\n Computer");
        int tot = 0;
        if (cTotal <= 100) {
            while (tot < 20) {
                Comp.roll();
                System.out.println(Comp);

                if (Comp.getDie1FaceValue() == 1
                                || Comp.getDie2FaceValue() == 1) {
                    System.out.println("Moves to User");
                    game.User();
                } else if (Comp.getDie1FaceValue() == 1
                                && Comp.getDie2FaceValue() == 1) {
                    cTotal = 0;
                    System.out.println("Moves to User");
                    game.User();
                } else {
                    tot = tot + Comp.getTotalFaceValue();
                    cTotal = cTotal + Comp.getTotalFaceValue();
                    System.out.println("Round Total :" + tot);
                    System.out.println("Computer Total :" + cTotal);
                }
            }
        } else {
            play = false;
        }
    }
}

2 个答案:

答案 0 :(得分:3)

您的意思是java.util.Scanner而不是Scannerimport java.util.Scanner;

答案 1 :(得分:0)

import java.util.Scanner;这应该工作