java方法返回int_java – 返回int值的Boolean方法;

我正在自学

Java,我创建了一个模拟掷骰子游戏的程序.我用布尔方法完成整个游戏.一切正常,但我想在每次用户获胜时计算.计算这些胜利然后打印出来的最佳方法是什么?

这是我的代码:

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;

}

}

}

}

我试图返回计数变量,但我得到一个错误,说我不能从我期望的布尔方法返回一个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).

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值