2016-10-05 10 views
-4

私のプログラムはユーザに名前を尋ね、3つの乱数のシャッフルを要求します。基本的なJavaプログラムのチューンアップの手助け

シャッフルのいずれかが目的の数(31)に追加されると、シャッフルが停止します。私はプログラムが最後のシャッフルだけを読むということが必要です。例えば。あなたがしたいですかどのように多くのシャッフル

:あなたが勝者です3

10 + 11 + 10 = 31おめでとうを!

電流出力である:

9 + 6 + 8

8 + 10 + 12

7 + 9 + 11

私は支援が必要ユーザーが名前にアルファベット以外の文字を使用できないようにします。数字が印刷されるまでにユーザーが持っていたシャッフルの数を印刷する機能も必要です。ここで

は、最後のシャッフルが勝者か敗者として読み取る得ることができることを確認するために

`import java.util.InputMismatchException; 
    import java.util.Scanner; 
    import java.io.IOException; 
    import java.util.Random; 

public class StringVariables { 



public static void main(String[] args) throws NumberFormatException, 
     IOException { 

    // user inputs their name in this section 
    Scanner user_input = new Scanner(System.in); 
//enter their first name 
    String first_name; 
    System.out.print("Enter Your First Name: "); 
    first_name = user_input.next(); 
    //enter their last name 
    String last_name; 
    System.out.print("Enter Your Last Name: "); 
    last_name = user_input.next(); 
//full name printed together 
    String full_name; 
    full_name = first_name + " " + last_name; 

    System.out.println(full_name + " Is Now Playing"); 

    // this is the shuffle portion as well as something to see if a number 

    int numShuffles = -1; 
    while (numShuffles < 0) { 

     System.out.println("How many times do you want the numbers shuffled? "); 

     try { 
      numShuffles = user_input.nextInt(); 
     } catch (InputMismatchException inputException) { 
      System.out.print("Please enter a valid number. \n"); 
      //this is the buffer that resets if the user types a letter instead of a number, or any other character 
      user_input.next(); 
     } 
    } 

    // here is going to be the loop for shuffles 

    // we are now going to generate their random number and add a delay 
    // after completing their name fields 

    delay(3000); 
    System.out 
      .println(" You will be given a hand of 3 random numbers between 7-13" 
        + "\n you will be drawn a the number of shuffles as you entered above "); 

    delay(2000); 
    System.out 
      .println(" Then, the computer will add the random numbers and if it is equal to 31, you win."); 

    /* 
    * end of explanation of the game, next i will create a new screen with 
    * the user's name and numbers 
    */ 

    delay(4000); 
    // printing 25 blank lines 
    for (int i = 0; i < 25; i++) 
     System.out.println(" "); 


    System.out.println("User playing: " + full_name); 

    System.out.println("Your lucky numbers are..."); 

    // random number generator 

    Random random = new Random(); 



    while (true) { 

     // the shuffle loop 
     boolean isWinner = false; 
     for (int i = 0; i < numShuffles; i++) { 
      int num1 = 7 + random.nextInt(7); 
      int num2 = 7 + random.nextInt(7); 
      int num3 = 7 + random.nextInt(7); 

      System.out.println(num1 + " + " + num2 + " + " + num3 + " = " 
        + (num1 + num2 + num3)); 

    // adding the numbers together   

      if (num1 + num2 + num3 == 31) { 
       isWinner = true; 
       System.out 
         .println("Congratulations !! You are the Lucky Winner !!!!"); 
       break; 
       //if you loose every shuffle 
      } 
     } 
     if (!isWinner) 
      System.out.println("Better Luck Next Time"); 

     // play again prompt 
     System.out 
       .println(" Do you want to play again? (If you do enter y or yes) \n To exit press any other key "); 
     String input = user_input.next(); 
     if (!"y".equalsIgnoreCase(input) && !"yes".equalsIgnoreCase(input)) { 
      break; 
     } 
    } 

    // if pressed y or yes the program will run again with the same number of shuffles entered from before 
    user_input.close(); 
} 

// delay field 

public static void delay(int millis) { 
    try { 
     Thread.sleep(millis); 
    } catch (InterruptedException exp) { 

     // delay field 

    } 
} 
}` 
+0

重複している可能性があるので、シャッフルではありません。例えば7,7,7。 –

+0

このプロジェクトで重複している数字は大丈夫です@PeterLawrey –

+0

あなたが手助けできることは何ですか? –

答えて

1
Arraylist numberStore = new Arraylist(); 
for (int i = 0; i < numShuffles; i++) { 
    int num1 = 7 + random.nextInt(7); 
    int num2 = 7 + random.nextInt(7); 
    int num3 = 7 + random.nextInt(7); 

    System.out.println(num1 + " + " + num2 + " + " + num3 + " = " + (num1 + num2 + num3)); 

    numberStore.add(num1 + num2 + num3); 

} 

int lastNumber = (numberStore.size() - 1); 
if (lastNumber == 31) { 
     isWinner = true; 
     System.out 
       .println("Congratulations !! You are the Lucky Winner !!!!"); 
     break; 
     //if you loose every shuffle 
} 

、私のコードです。

forループ内でnum1、num2 ...の変数を初期化するため、これらの変数はforループのスコープになります。私は、あなたがループの範囲を外に移動すると、1組の数字しか判断できないようにしたいと思っているならば、それを提案します。配列に合計を追加することで、あなたが判断したい数の配列を選択することができます。それはあなたの入力を殺菌することになると

あなたが正規表現の少しの知識を持ってあなたのためにそれのほとんどを行うためにutil.Scannerを使用することができます。これはどのなしアルファベット文字のは、入力されている可能スキャナを停止します

while (!scan.hasNext("[A-Za-z]+")) { 
    System.out.println("Nope, that's not it!"); 
    sc.next(); 
} 

、あなたRegexの詳細について読むことができますthis tool

+0

ありがとう!私はコードをテストしたが、ループが終了する前に答えが正しいとシャッフルループを停止する。それ以上の提案を –

+0

あなたは私がコードに実装することができると思いますか?私は脳のおならの日を過ごしていて、私の人生を考えることができません –

+0

ループがしていることは、3つの数字を生成し、それが31になるかどうかをチェックすることです。 あなたがする必要があるのは、あなたのループの外側にあり、最後の3つを確認してください。私が仕事から家に帰るのに40分をくれば、それまでに仕事をしていないなら、それを実装するのを手伝ってくれるでしょう。 –

関連する問題