我试图将我的sum变量设置为在初始化之后保持不变,但是当我重复使用num1和num2时,它会重置sum,尽管“final int sum”。此外,我不想经历制作两个骰子方法(并且不知道如何)的麻烦,因此使得总和不变是我所需要的。
package crapsapp;
public class CrapsApp {
public static void main(String[] args) {
int num1 = 0;
int num2 = 0;
int roll = 0;
boolean flag = true;
while(flag){
roll++;
num1 = getDice(1,6);
num2 = getDice(1,6);
final int sum = (num1 + num2);
if(roll == 1 ){
switch(sum){
case 2: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1 + num2)+ ", you lost..");flag=false;break;
case 3: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1 + num2)+ ", you lost..");flag=false;break;
case 12: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1 + num2)+ ", you lost..");
flag=false;
break;
case 7: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1 + num2)+ ", you won!"); flag=false;break;
case 11: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1 + num2)+ ", you won!");
flag=false;
break;
case 4: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(sum)+ "... you roll again.");break;
case 5: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(sum)+ "... you roll again.");break;
case 6: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(sum)+ "... you roll again.");break;
case 8: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(sum)+ "... you roll again.");break;
case 9: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(sum)+ "... you roll again.");break;
case 10: System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(sum)+ "... you roll again.");
roll++;
break;
}//end of switch
}//end of if
else if(roll==3) {
while(num1+num2!=sum||num1+num2!=7){
num1 = getDice(1,6);
num2 = getDice(1,6);
System.out.println(num1+" "+num2+" "+sum+" ");
if(num1+num2!=sum&&num1+num2!=7){
System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1+num2)+ "... you roll again.");
}
else if(num1+num2==7){
System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1+num2)+ ", you lost..");
System.exit(0);
}//end of if
//end of else if
else if(num1+num2==sum) {
System.out.println("You rolled " +num1+ " and " +num2+ ". The sum of your two numbers is " +(num1+num2)+ ", you won!");
System.exit(0);
}//end of else if
}//end of while
}//end of else if
}//end of while
}//end of main
public static int getDice(){
int num1;
num1 = (1+ (int)(Math.random() *6));
return num1;
}
public static int getDice(int min, int max){
int num2;
num2 = (1+ (int)(Math.random() *6));
return num2;
}
}
答案 0 :(得分:1)
将int sum = -1;
移至while(flag)
然后在循环中执行
if (sum != -1) {
sum = (num1 + num2);
}
看起来有点奇怪,你想要这样做