2017-07-14 5 views
-1

私はsum変数を初期化した後も同じままにしようとしていますが、num1とnum2を再利用すると "final int sum"にもかかわらず合計がリセットされます。また、私は2つのダイスメソッドを作るという問題を抱えていません。私の変数を一定に保つのに問題がある

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ループの直後に新しいif文を作成すると、sumが-1のままで動作しないようです。 – briggleshiggle

関連する問題