2017-07-14 50 views
-1

我試圖設置我的sum變量在初始化後保持不變,但是當我重新使用num1和num2時,儘管出現了「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; 
    } 
} 

回答

1

移動int sum = -1;到前while(flag)

然後在循環做

if (sum != -1) { 
    sum = (num1 + num2); 
} 

似乎有點怪,你想這樣做雖然

+0

我應該做一個新的,如果聲明或使用頂部?如果不是,那麼while循環會進入嗎?當我在第一個while循環下創建一個新的if語句時,似乎並不像-1那樣工作。 – briggleshiggle