XXX

代码大整合

package baidu.dice.all;

import java.util.Scanner;

public class DiceRollerApp {

    // 验证,其他需求可继续添加
    static class Validator {

        public static void validator(int value1, int value2) {
            if ((value1 == 2 && value2 == 5) || (value2 == 2 && value1 == 5)) {
                System.out.println("Craps! ");
            }
            if ((value1 == 6) && (value2 == 6)) {
                System.out.println("Box cars! ");
            }
            if ((value1 == 1) && (value2 == 1)) {
                System.out.println("Snake eyes! ");
            }
        }
    }

    // 骰子实体类
    class Dice {

        // 骰子点数
        private int sides;

        // 默认构造方法
        public Dice() {
        }

        // 带参构造方法
        public Dice(int sides) {
            this.setSides(sides);
        }

        // 获取随机点数
        public int getValue() {
            // 调用数学函数,获取0-5随机数
            int value = (int) (Math.random() * 6);
            // +1之后即可获取1-6随机数
            return (value + 1);
        }

        // 投掷骰子
        public void roll() {
            this.sides = this.getValue();
            System.out.println(this.sides);
        }

        // Getter/Setter方法
        public int getSides() {
            return sides;
        }

        public void setSides(int sides) {
            this.sides = sides;
        }
    }

    // 储存2个不同骰子
    class PairOfDice {

        private Dice dice1 = new Dice();

        private Dice dice2 = new Dice();

        public PairOfDice() {
        }

        public PairOfDice(int sides) {
            // TODO 不知道什么意思,需要实现什么功能?
        }

        // 获取第一个骰子的点数
        public int getValue1() {
            int value = dice1.getSides();
            return value;
        }

        // 获取第二个骰子的点数
        public int getValue2() {
            int value = dice2.getSides();
            return value;
        }

        // 投掷两个骰子
        public void roll() {
            dice1.roll();
            dice2.roll();
        }

        // 获取点数之和
        public int getSum() {
            int sum = this.getValue1() + this.getValue2();
            return sum;
        }
    }

    public static void main(String[] args) {

        // 内部类实例化
        PairOfDice dices = new DiceRollerApp().new PairOfDice();
        // 声明键盘输入
        Scanner scanner = new Scanner(System.in);
        // 累计投骰子次数
        int count = 0;
        System.out.println("欢迎来玩");
        // 声明是否循环的标识
        boolean isNext = true;

        while (isNext) {
            System.out.println("投骰子吗?(y/n): y");
            // 键盘输入
            String input = scanner.next();
            if ("y".equalsIgnoreCase(input)) {// 输入为"y"
                System.out.println("第" + (++count) + "次:");
                // 投骰子,打印点数
                dices.roll();
                // 调用静态方法,不需要实例化类
                Validator.validator(dices.getValue1(), dices.getValue2());
            } else if ("n".equalsIgnoreCase(input)) {// 输入为"n"
                System.out.println("按任意键继续。。。退出本程序请输入f");
                input = scanner.next();
                if ("f".equalsIgnoreCase(input)) {
                    isNext = false;
                }
            } else {// 其他字符输入
                System.out.println("输入的选择不正确!(y/n)");
            }
        }
        System.out.println("欢迎下次来玩!");

    }

}


分开

package baidu.dice;

/**
 * 骰子实体类
 * 
 * @author 莫小哆_ly
 * 
 */
public class Dice {

    // 骰子点数
    private int sides;

    // 默认构造方法
    public Dice() {
    }

    // 带参构造方法
    public Dice(int sides) {
        this.setSides(sides);
    }

    // 获取随机点数
    public int getValue() {
        // 调用数学函数,获取0-5随机数
        int value = (int) (Math.random() * 6);
        // +1之后即可获取1-6随机数
        return (value + 1);
    }

    // 投掷骰子
    public void roll() {
        this.sides = this.getValue();
        System.out.println(this.sides);
    }

    // Getter/Setter方法
    public int getSides() {
        return sides;
    }

    public void setSides(int sides) {
        this.sides = sides;
    }
}

package baidu.dice;

/**
 * 储存2个不同骰子
 * 
 * @author 莫小哆_ly
 * 
 */
public class PairOfDice {

    private Dice dice1 = new Dice();

    private Dice dice2 = new Dice();

    public PairOfDice() {
    }

    public PairOfDice(int sides) {
        // TODO 不知道什么意思,需要实现什么功能?
    }

    // 获取第一个骰子的点数
    public int getValue1() {
        int value = dice1.getSides();
        return value;
    }

    // 获取第二个骰子的点数
    public int getValue2() {
        int value = dice2.getSides();
        return value;
    }

    // 投掷两个骰子
    public void roll() {
        dice1.roll();
        dice2.roll();
    }

    // 获取点数之和
    public int getSum() {
        int sum = this.getValue1() + this.getValue2();
        return sum;
    }
}

package baidu.dice;

/**
 * 验证
 * 
 * @author 莫小哆_ly
 * 
 */
public class Validator {

    // 验证,其他需求可继续添加
    public static void validator(int value1, int value2) {
        if ((value1 == 2 && value2 == 5) || (value2 == 2 && value1 == 5)) {
            System.out.println("Craps! ");
        }
        if ((value1 == 6) && (value2 == 6)) {
            System.out.println("Box cars! ");
        }
        if ((value1 == 1) && (value2 == 1)) {
            System.out.println("Snake eyes! ");
        }
    }
}

package baidu.dice;

import java.util.Scanner;

/**
 * 投掷骰子
 * 
 * @author 莫小哆_ly
 * 
 */
public class DiceRollerApp {

    public static void main(String[] args) {

        // 内部类实例化
        PairOfDice dices = new PairOfDice();
        // 声明键盘输入
        Scanner scanner = new Scanner(System.in);
        // 累计投骰子次数
        int count = 0;
        System.out.println("欢迎来玩");
        // 声明是否循环的标识
        boolean isNext = true;

        while (isNext) {
            System.out.println("投骰子吗?(y/n): y");
            // 键盘输入
            String input = scanner.next();
            if ("y".equalsIgnoreCase(input)) {// 输入为"y"
                System.out.println("第" + (++count) + "次:");
                // 投骰子,打印点数
                dices.roll();
                // 调用静态方法,不需要实例化类
                Validator.validator(dices.getValue1(), dices.getValue2());
            } else if ("n".equalsIgnoreCase(input)) {// 输入为"n"
                System.out.println("按任意键继续。。。退出本程序请输入f");
                input = scanner.next();
                if ("f".equalsIgnoreCase(input)) {
                    isNext = false;
                }
            } else {// 其他字符输入
                System.out.println("输入的选择不正确!(y/n)");
            }
        }
        System.out.println("欢迎下次来玩!");

    }
}


你可能感兴趣的:(String,Class,input)