2016-03-25 18 views
0

私はこのスロットマシンを私のプログラミングクラス用に作っていますが、プログラムを再実行して無限ループを起こすことなく再実行するかどうか尋ねる方法はわかりません。どんな助けもありがとう。Slot Machine using methods

public static void main(String[] args) 
{ 
    boolean playAgain = true; 
    Scanner input = new Scanner(System.in); 
    System.out.println("Welcome, you have 10 credits! Play (Y/N)"); 
    String choice; 
    choice = input.nextLine(); 
    int credits = 10; 
    if ("y".equals(choice)) 
    { 
     playAgain = true; 
    } else { 
     playAgain = false; 
    } 
    while (playAgain = true) 
    { 
     int sp0 = spin(); 
     int sp1 = spin(); 
     int sp2 = spin(); 
     System.out.println(sp0 +"\t"+ sp1 +"\t"+ sp2); 
     int answer = evaluate(sp0,sp1,sp2); 
     int newCred = answer + credits - 1; 
     System.out.println("Your now have "+ newCred + " credits"); 
     System.out.println("Press y to play again"); 
     String newC = input.nextLine(); 


    } 


// TODO code application logic here 
} 
public static int spin() 
{ 
    int num1; 
    Random rand = new Random(); 
    num1 = rand.nextInt(8); 
    return num1; 


} 
public static int evaluate(int e1, int e2, int e3) 
{ 
    int num; 
    if(e1==0 && e2==0 && e3==0) 
    { 

     num= 7; 
     System.out.println("You Win"); 

    }else if (e1==e2 && e2 == e3){ 
     num = 5; 
     System.out.println("You Win"); 

    }else if (e1== 0 && e2==0 && e3 != 0){ 
     num= 3; 
     System.out.println("You win"); 

    } else if (e1==e2 && e2==e1 && e3 != e1){ 
     num= 2; 
     System.out.println("You win"); 

    } else if (e1==0 && e2 !=0 && e3 != 0){ 
     num= 1; 

     System.out.println("You win"); 
    } else { 
     num =0; 
     System.out.println("You Lose"); 
    } 

    return num; 
} 
+0

あなたは変数が 'playAgain'が_inside_変化していることを確認する必要があるかもしれませループも? – mustaccio

答えて

2

この行は、おそらくあなたが期待何をしていません。

while (playAgain = true) 

=オペレータは==は比較のため、割り当てのためです。 したがって、この行はplayAgainの値をtrue, に設定し、式の値はtrueであり、 です。したがって、これは無限ループです。あなたがたの書き込みに何を意味するのか

は次のとおりです。

while (playAgain == true) 

しかし、あなたが直接ブール式を使用することができますので、あなたは、このように、全く==オペレータを必要としませんでした:

while (playAgain) 

しかし、これはたりない。 whileループの本体の内部には、playAgainという値を変更するものはありません。 一つの方法は、これを修正するには:

while (playAgain) { 
    int sp0 = spin(); 
    int sp1 = spin(); 
    int sp2 = spin(); 
    System.out.println(sp0 +"\t"+ sp1 +"\t"+ sp2); 
    int answer = evaluate(sp0,sp1,sp2); 
    int newCred = answer + credits - 1; 
    System.out.println("Your now have "+ newCred + " credits"); 
    System.out.println("Press y to play again"); 
    playAgain = "y".equals(input.nextLine()); 
} 
+0

'playAgain'も' newC'に基づいてループ内で再割り当てする必要があります。 –

+0

ありがとう@MickMnemonic、私はそれも気づいた、今すぐ固定 – janos

+0

あなたは人生の保護者です – JoBrien

0
playAgain = true 

がplayAgainに真割り当て

playAgain == true 

テストplayAgainに等しい真