【发布时间】:2014-10-16 22:28:43
【问题描述】:
如何编辑我的代码以包含一个名为 rollDice() 的方法,以返回生成的介于 1 和 6 之间的随机整数?谢谢。
这是我当前的代码:
public class CrapsGame {
public static void main(String[] args) {
int dice1 = (int)(Math.random()* 6) + 1;
int dice2 = (int)(Math.random()* 6) + 1;
int roll = dice1 + dice2;
System.out.println();
System.out.print("You rolled "+roll+". ");
if(roll == 2 || roll == 3 || roll == 12){
System.out.println("You lose");
}
else if(roll == 7 || roll == 11){
System.out.println("You win");
}
else{
System.out.println("point is "+roll+"\n");
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
int roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
while(roll2 != 7){
if(roll == roll2){
System.out.println("You win");
break;
}
else{
System.out.println("point is "+roll+"\n");
}
dice1 = (int)(Math.random()* 6) + 1;
dice2 = (int)(Math.random()* 6) + 1;
roll2 = dice1 + dice2;
System.out.print("You rolled "+roll2+". ");
}
if(roll2 == 7){
System.out.println("You lose");
}
}
}
}
【问题讨论】:
-
你可以写这一切,但你不知道如何创建一个方法?而且您还没有通过阅读有关 StackOverflow 的其他问题或其中一千个 Java 教程找到任何帮助?
-
我目前正在学习方法,我之前已经使用过循环。