简体   繁体   English

返回int值的Boolean方法;

[英]Boolean method that returns an int value;

I am teaching myself Java, and I created a program to simulate a game of craps. 我正在自学Java,我创建了一个模拟掷骰子游戏的程序。 I have the entire game in a Boolean method. 我用布尔方法完成整个游戏。 Everything works fine, but I want to count every time the user wins. 一切正常,但我想在每次用户获胜时计算。 What is the best way to count these wins, and then print them? 计算这些胜利然后打印出来的最佳方法是什么?

Here is my code: 这是我的代码:

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;
        }
    }
}
}

I have tried to return the count variable but I get an error saying I can't return an int value from a Boolean method which I expected. 我试图返回计数变量,但我得到一个错误,说我不能从我期望的布尔方法返回一个int值。 But I also tried to print out the count value with System.out.println(); 但我也试图用System.out.println();打印出计数值System.out.println(); But I can't find a place in the code to put it since I always get an error stating that the println is unreachable code. 但是我无法在代码中找到一个位置来放置它,因为我总是得到一个错误,指出println是无法访问的代码。

Any help would be appreciated. 任何帮助,将不胜感激。 Thanks! 谢谢!

Here is the output of the program 这是程序的输出

[1,6] 7 you win [1,6] 7你赢了

[2,4] point= 6[3,6][3,3] 6 you win [2,4]分= 6 [3,6] [3,3] 6你赢了

[3,3] point= 6[5,1] 6 you win [3,3]分= 6 [5,1] 6你赢了

[6,1] 7 you win [6,1] 7你赢了

[6,3] point= 9[5,1][3,5][2,6][6,4][5,6][1,5][6,3] 9 you win [6,3]分= 9 [5,1] [3,5] [2,6] [6,4] [5,6] [1,5] [6,3] 9你赢了

[3,3] point= 6[1,5] 6 you win [3,3]分= 6 [1,5] 6你赢了

[5,1] point= 6[1,5] 6 you win [5,1]分= 6 [1,5] 6你赢了

[2,2] point= 4[6,4][3,3][3,4][3,4] 7 you lose [2,2]点= 4 [6,4] [3,3] [3,4] [3,4] 7你输了

[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 [4,5]点= 9 [6,4] [5,1] [1,2] [6,2] [1,5] [3,5] [6,5] [1,5] [5 ,2] [5,2] 7你输了

[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 [2,2]点= 4 [5,3] [5,6] [6,4] [3,2] [6,5] [3,6] [4,4] [4,2] [4 ,3] [4,3] 7你输了

[6,6] 12 you lose [6,6] 12你输了

You won 11 time(s). 你赢了11次。

Count them outside the craps function, here's one simple way: 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!");
}

By the way, it's important to note that it's far superior to count outside the craps function than it is to modify it. 顺便说一下,重要的是要注意它在craps功能之外的数量远远超过修改它的数量。 As you learn java, remember that each method should only do one job. 在学习java时,请记住每个方法只能完成一项工作。 You may want to practice breaking the craps function into smaller pieces to practice this philosophy. 您可能希望练习将craps功能分解为更小的部分来实践这一理念。 The one method to one job philosophy will make larger projects much easier to understand and debug when you are looking back later. 一种工作原理的一种方法将使您在稍后回顾时更容易理解和调试更大的项目。

int wins = 0;
for(int i = 0; i <= 10; i++){
    if(craps(rand)) {
        wins++;
    }
}

// Print a friendly message including `wins`

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

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