繁体   English   中英

java 遇到的计数器问题

[英]counter issue encountered with java

我正在做一个骰子游戏,规则是随机掷2个骰子,如果第一次尝试两个骰子的总和为7或11,则用户获胜。 如果第一次尝试总和为 2,3 或 12,则用户输了。 如果总和是 4,5,6,8,9,10,则该总和被设置为建立点并继续掷骰子,直到总和与建立点匹配并且用户获胜,但是如果建立点是 7用户输了。

游戏进行了 3 次,并记录了我赢了或输了多少次。 但是,我无法完成该部分,不胜感激,我的代码如下。

package roll;

import java.util.Random;

public class RandomSumGame {

    boolean start;
    int d1;
    int d2;
    int sum = d1 + d2;
    int valuepoint;
    String plus = "+";

    public void play(int d1, int d2) {
        int win = 0;
        int loss = 0;
        sum = d1 + d2;

        for (;;) {
            if (sum == 2 || sum == 3 || sum == 12) {
                start = false;
                loss++;
                System.out.println(" = " + sum + ";you lose");
                break;

            } else if (sum == 7 || sum == 11) {
                start = true;
                win++;
                System.out.println(" = " + sum + ";you win ");
                break;
            }

            valuepoint = sum;

            if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
                    || valuepoint == 10) {
                System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
                break;

            }

        }
        for (;;) {
            if (sum == 7 || sum == 11) {
                break;

            } else {
                Random rand = new Random();
                for (int i = 0; i < 1; i++) {
                    d1 = 1 + rand.nextInt(6);
                    int f1 = d1;
                    System.out.print("\t" + "-you rolled again " + d1 + " " + plus);

                    for (int x = 0; x < 1; x++) {
                        d2 = 1 + rand.nextInt(6);
                        int f2 = d2;
                        int loda = f1 + f2;
                        System.out.print(" " + d2 + " = " + loda + "\n");

                    }

                }
                sum = d1 + d2;

                if (sum == valuepoint) {
                    start = true;
                    win++;

                    System.out.print("\t" + "you win!" + "\n");
                    break;

                } else if (sum == 7) {
                    loss++;
                    start = false;
                    System.out.print("\t" + "you lose " + "\n");
                    break;

                }
            }

        }
        System.out.print("wins: " + win + "\n");
        System.out.print("Losses:" + loss);
    }

    public void play() {

        for (int x = 0; x < 3; x++) {

            rolldice();
            play(d1, d2);
            System.out.print("\n");

        }

    }

    public void rolldice() {
        Random rand = new Random();

        for (int i = 0; i < 1; i++) {
            d1 = 1 + rand.nextInt(6);
            System.out.print("You rolled " + d1 + " " + plus);

        }
        for (int i = 0; i < 1; i++) {
            d2 = 1 + rand.nextInt(6);
            System.out.print(" " + d2 + " ");
        }

    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        RandomSumGame Teet = new RandomSumGame();
        RandomSumGame d1counter = new RandomSumGame();
        RandomSumGame d2counter = new RandomSumGame();

        Teet.play();

    }
}

哪一部分你不能完成? 好像游戏运行了3次? 是输赢的总数吗? 要解决这个问题,您可以将“int win”和“int loss”移到 play 方法之外,使其成为全局变量。

编辑:如果您不想使用全局变量,您可以创建一个调用自身的方法(递归方法)


import java.util.Random;

public class RandomSumGame {
    boolean start;
    int d1;
    int d2;
    int sum = d1 + d2;
    int valuepoint;
    String plus = "+";

    public void play(int d1, int d2, int win, int loss) {
        sum = d1 + d2;

        for (;;) {
            if (sum == 2 || sum == 3 || sum == 12) {
                start = false;
                loss++;
                System.out.println(" = " + sum + ";you lose");
                break;

            } else if (sum == 7 || sum == 11) {
                start = true;
                win++;
                System.out.println(" = " + sum + ";you win ");
                break;
            }

            valuepoint = sum;

            if (valuepoint == 4 || valuepoint == 5 || valuepoint == 6 || valuepoint == 8 || valuepoint == 9
                    || valuepoint == 10) {
                System.out.print(" = " + valuepoint + " you establish a value point " + valuepoint + "\n");
                break;

            }

        }
        for (;;) {
            if (sum == 7 || sum == 11) {
                break;

            } else {
                Random rand = new Random();
                for (int i = 0; i < 1; i++) {
                    d1 = 1 + rand.nextInt(6);
                    int f1 = d1;
                    System.out.print("\t" + "-you rolled again " + d1 + " " + plus);

                    for (int x = 0; x < 1; x++) {
                        d2 = 1 + rand.nextInt(6);
                        int f2 = d2;
                        int loda = f1 + f2;
                        System.out.print(" " + d2 + " = " + loda + "\n");

                    }

                }
                sum = d1 + d2;

                if (sum == valuepoint) {
                    start = true;
                    win++;

                    System.out.print("\t" + "you win!" + "\n");
                    break;

                } else if (sum == 7) {
                    loss++;
                    start = false;
                    System.out.print("\t" + "you lose " + "\n");
                    break;

                }
            }

        }

        System.out.print("wins: " + win + "\n");
        System.out.print("Losses:" + loss);
        if (win < 2 && loss < 2) {
            System.out.print("\n");
            //Recursive
            play(win, loss);
        }
    }

    public void play(int win, int loss) {
        rolldice();
        play(d1, d2, win, loss);
    }

    public void rolldice() {
        Random rand = new Random();

        for (int i = 0; i < 1; i++) {
            d1 = 1 + rand.nextInt(6);
            System.out.print("You rolled " + d1 + " " + plus);

        }
        for (int i = 0; i < 1; i++) {
            d2 = 1 + rand.nextInt(6);
            System.out.print(" " + d2 + " ");
        }
    }

    public static void main(String[] args) {
        RandomSumGame test = new RandomSumGame();
        test.play(0,0);
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM