2017-03-13 21 views
1

このプログラムは、ユーザーから2つの数字を取得するとします。それを高いか低いかのように指示すれば、順序は重要ではない。次に、コンピュータに乱数を生成させ、ユーザが一致するかどうかを確認するために数字を入力する必要があります。ユーザー番号が低すぎたり高すぎたりすると、私のプログラムが表示されます。私のコードにplayAgainループを追加するのに助けが必要

私が助けが必要なのは、このコードの中にplayAgainループを置くことです。 「もう一度やりたいですか(Y/N)」と言うべきです。その後、ユーザーの文字列またはcharが取得され、プログラムが再び実行されるか、プログラムが終了します(ユーザーの入力内容に応じて)。

私は何時間も働いて研究してきましたが、道を見つけることができないようです(たぶん、私は高密度です)。申し訳ありませんが、この質問を2回目に投稿した場合、今日の終わりまでにこれを行う必要があります。

import java.util.*; 

/* 
Guessing Game Group Project. This game will ask for two numbers to set the range, 
*/ 

public class Guess1 
{ 
    public static void main(String [] args) 
    { 
     Scanner S = new Scanner(System.in);                                //Introduction Statement to game 
     System.out.println("This is a guessing game. I will ask you for two numbers."); 
     System.out.println("I will choose a random number between your two numbers for you " + "\n" + "to try and guess. As you guess, I will give you hints."); 

     System.out.print("Choose two numbers to bound your range: ");                         //Scanner to store user's input for the range 
     int u1 = S.nextInt(); 
     int u2 = S.nextInt(); 
     int guess; 
     int count = 0; // = 0 new 
     String playAgain; 

     int high; 
     int low; 
     if(u1 < u2) 
     { 
      high = u2; 
      low = u1; 
     } 
     else 
     { 
      high = u1; 
      low = u2; 
     } 
     Random gen = new Random(); 
     int rand = gen.nextInt(high-low+1) + low; 
     System.out.println("Now guess a number between " + low + " and " + high + ": "); 
     guess = S.nextInt(); 
     while(guess != rand) 
     { 
      count++; 
      if (guess > high || guess < low) 
      { 
       System.out.println("Out of range. Please follow the directions dumb-ass."); 
      } 
      else if(guess > rand) 
      { 
       high = guess; 
       System.out.println("Too high!"); 
      } 
      else 
      { 
       low = guess; 
       System.out.println("Too low!"); 
      } 

      System.out.print("Now guess a number between " + low + " and " + high + ": "); 
      guess = S.nextInt(); 

      if(guess == rand) 
      { 
       System.out.println("You got it!"); 
      } 
      else if(count == 10) 
      { 
       System.out.println("You lost! So Sorry."); 
      } 
     } 

    } 
} 

答えて

0

続きの質問をするゲームの本体の周りに2番目のループが必要です。プレイヤーが「いいえ」と答えた場合は、終了することができます。 Scannerのjavaドキュメントを参照してください。

関連する問題