2016-05-18 4 views
1

私はしばらくプログラムしていないし、物事の振りを取り戻そうとしています。私の質問は、どのように私は2番目の質問をループするので、応答がはいまたはいいえのほかに何かであれば、質問をもう一度尋ねます。私はifステートメントの周りにループを入れようとしましたが、ユーザーから別の応答を取得しようとするたびに、変数の応答を使用することができません。私はこれがループを理解するのは簡単な修正だと思っていますが、私はこの特定の問題について私の頭を包み込むのに苦労しています。Javaのループロジック

import java.util.Scanner; 
public class Practice { 

    public static void main(String[] args) { 
     Scanner input = new Scanner(System.in); 
     System.out.println("Welcome to my simulation, please enter your name"); 
     String name = input.nextLine(); 
     System.out.println("Welcome " + name + " would you like to play a game?"); 
     String response = input.nextLine(); 


     boolean yes = new String("yes").equals(response.toLowerCase()); 
     boolean no = new String("no").equals(response.toLowerCase()); 


     if (yes){ 
      System.out.println("Which game woudl you like to play?"); 
     }else if (no){ 
      System.out.println("Fine then, have a good day!"); 
     } 
     else{ 
      System.out.println("please enter either yes or no"); 
     } 
    } 

} 
+2

'新しい文字列( "はい")'を使用しないでください。 ''はい ''はうまくいくでしょう。 – shmosel

+1

物事の「スイング」..heh .. – Eric

+0

@エリックですが...この質問にはスイングはありません。 – shmosel

答えて

5

これにはさまざまな方法があります。ここで頭に浮かんだその最初です:

while (true) { 
    response = input.nextLine().toLowerCase(); 
    if (response.equals("yes") { 
     System.out.println("Which game woudl you like to play?"); 
     break; 
    } else if (response.equals("no") { 
     System.out.println("Fine then, have a good day!"); 
     break; 
    } else { 
     System.out.println("please enter either yes or no"); 
    } 
} 
+0

私はループ内で私のブーリアンステートメントを持っていなければなりませんでしたが、その後は魅力的なように働いていました。本当にありがとう! –

-1

どの程度

do{ 
    String response = input.nextLine(); 
    boolean yes = new String("yes").equals(response.toLowerCase()); 
    boolean no = new String("no").equals(response.toLowerCase()); 
    if (yes){ 
     System.out.println("Which game woudl you like to play?"); 
    }else if (no){ 
     System.out.println("Fine then, have a good day!"); 
    } 
    else{ 
     System.out.println("please enter either yes or no"); 
    } 
}while(!response.equals("yes") && !response.equals("no")); 
+0

私はdownvoteを持っている理由を知りたいです –

+2

私はdownvoteしませんでした。しかし、あなたのコードは応答が決して変化しないので、一度だけまたは無限ループするでしょう。 –

+0

@ 911didbush良いキャッチ! –

-4
while(response =! yes || response || no) 

これは役立つはず!

+0

http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – sinclair

+0

'='!私が知っている任意の言語での有効な演算子ではありません。また、これは信じられないほど、すばらしいことに、魅力的に間違っているようです。その他の質問に答える前に、[この基本的なJavaチュートリアル](https://docs.oracle.com/javase/tutorial/getStarted/application/index.html)を読むことをお勧めします。 –

0

まず第一に、自分のことを容易にするために、あなたはboolean変数を使用する必要はありません。 .equals()メソッドを使用して、応答がyesまたはnoと等しいかどうかをテストできます。この場合、次のようなelse文を使用する方が簡単です。

if (response.equals("Yes")) 
{ 
    System.out.println("Which game woudl you like to play?"); 
} 

else if (response.equals("No")) 
{  
    System.out.println("Fine then, have a good day!"); 
} 

else if (!response.equals("Yes") && !response.equals("No")) 
{ 
    while (!response.equals("Yes") && !response.equals("No")) 
    { 
     System.out.println("please enter either yes or no"); 
     response = input.nextLine(); 
    } 
} 

希望はあなたにとって役に立ちました。

+0

私は知っている、私はあなたがそうすることができないかどうかわからなかったので、私は私のブールの使用を練習していたので、 –

+1

私は理解しています。それができる多くの方法があります。そこにはこのようなものの世界があります。私が去年かそこらで見つけたので、実践するのは本当に良い –

関連する問題