java - 返回 int 值的 boolean 方法;

标签 java boolean return

我正在自学 Java,并创建了一个程序来模拟掷骰子游戏。我在 boolean 方法中拥有整个游戏。一切正常,但我想计算每次用户获胜的次数。计算这些胜利并打印它们的最佳方法是什么?

这是我的代码:

import java.util.Random;

public class game{

public static void main(String[] args) {
    Random rand = new Random();
    for(int i = 0; i <= 10; i++){
        craps(rand);
    }
}

public static boolean craps(Random randomGen){
    int roll1 = randomGen.nextInt(6) + 1;
    int roll2 = randomGen.nextInt(6) + 1;

    int sum = roll1 + roll2;
    int sumRepeat = sum;
    int count = 0;


    String win = "you win";
    String lose = "you lose";
    String point = "point=";

    if(sum == 7 || sum == 11){
        System.out.printf("[%d,%d] %d %s\n",roll1,roll2,sum,win);
        count++;
        return true;
    }
    else if(sum == 2 || sum == 3 || sum == 12){
        System.out.printf("[%d,%d] %d %s\n",roll1,roll2,sum,lose);
        return false;
    }
    else{
        System.out.printf("[%d,%d] %s %d",roll1,roll2,point,sum);
        int roll3 = randomGen.nextInt(6) + 1;
        int roll4 = randomGen.nextInt(6) + 1;
        int sum2 = roll3 + roll4;
        do{
            roll3 = randomGen.nextInt(6) + 1;
            roll4 = randomGen.nextInt(6) + 1;
            sum2 = roll3 + roll4;
            if(sum2 == sumRepeat){
                System.out.printf("[%d,%d] %d %s\n",roll3,roll4,sum2,win);
                count++;
                return true;
            }else{
                System.out.printf("[%d,%d]",roll3,roll4);
            }
        }
        while(sum2 != 7);{
            System.out.printf("[%d,%d] %d %s\n",roll3,roll4,sum2,lose);
            return false;
        }
    }
}
}

我试图返回 count 变量,但我收到一条错误消息,提示我无法从我期望的 boolean 方法返回 int 值。但我也尝试用 System.out.println(); 打印出计数值,但我无法在代码中找到放置它的位置,因为我总是收到一个错误,指出 println是无法访问的代码。

如有任何帮助,我们将不胜感激。谢谢!

这是程序的输出

[1,6] 7 you win

[2,4] point= 6[3,6][3,3] 6 you win

[3,3] point= 6[5,1] 6 you win

[6,1] 7 you win

[6,3] point= 9[5,1][3,5][2,6][6,4][5,6][1,5][6,3] 9 you win

[3,3] point= 6[1,5] 6 you win

[5,1] point= 6[1,5] 6 you win

[2,2] point= 4[6,4][3,3][3,4][3,4] 7 you lose

[4,5] point= 9[6,4][5,1][1,2][6,2][1,5][3,5][6,5][1,5][5,2][5,2] 7 you lose

[2,2] point= 4[5,3][5,6][6,4][3,2][6,5][3,6][4,4][4,2][4,3][4,3] 7 you lose

[6,6] 12 you lose

You won 11 time(s).

最佳答案

craps 函数之外计算它们,这是一种简单的方法:

public static void main(String[] args) {
    Random rand = new Random();
    int counter = 0;
    for(int i = 0; i <= 10; i++){
        if(craps(rand)) counter++;
    }

    System.out.println("You won " + counter + " times!");
}

顺便说一下,重要的是要注意,在 craps 函数之外进行计数比修改它要好得多。在学习 Java 时,请记住每个方法只能完成一项工作。您可能想练习将 craps 函数分解成更小的部分来实践这一理念。 一种方法完成一项工作的理念将使更大的项目在您以后回顾时更容易理解和调试。

关于java - 返回 int 值的 boolean 方法;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13403055/

相关文章:

javascript - 在 JavaScript 中使用 "Boolean"作为 .filter() 的参数

mysql - 如何从MySQL表中查询复杂的 boolean 条件

java - 用于处理 Java 中 void 方法返回的虚拟变量?

JavaScript - Console.log 返回一些内容,但返回时显示未定义

Java序列生成不重复

java - 为什么我的选择排序算法不执行它应该执行的操作 (java)?

java - 非交互、非联网Android设备的升级机制

python - 从函数返回 boolean 值的最佳实践 - 硬编码或从变量

python - 在没有字典的情况下在 Python 中返回许多变量

java - 在 IntelliJ 中创建新包