2016-09-18 4 views
0

こんにちは皆さん、私は配列に問題があります。.nextInt();これは、3行目のプロンプトで私の出力行が下から上にシフトする原因となり、真剣に何が間違っているのか理解できません。Java配列とnextInt();

私は.hasNextInt();何もを試してみましたが、それは実際ので、ここでは、私にエラーを与えるコードです:

import java.util.Random; 
import java.util.Scanner; 

public class birthday { 

    public static void main(String[] args) { 
     System.out.println("Welcome to the birthday problem Simulator\n"); 
     String userAnswer=""; 
     Scanner stdIn = new Scanner(System.in); 
     do { 
      int [] userInput = promptAndRead(stdIn); //my problem 
      double probability = compute(userInput[0], userInput[1]); 

      // Print results 
      System.out.println("For a group of " + userInput[1] + " people, the probability"); 
      System.out.print("that two people have the same birthday is\n"); 
      System.out.println(probability); 

      System.out.print("\nDo you want to run another set of simulations(y/n)? :"); 

      //eat or skip empty line 
      stdIn.nextLine(); 
      userAnswer = stdIn.nextLine(); 
     } while (userAnswer.equals("y")); 

     System.out.println("Goodbye!"); 
     stdIn.close(); 
    } 

    // User input prompt where you make the simulation. For people and return them as an array 
    public static int[] promptAndRead(Scanner stdIn) 
    { 
     System.out.println("Please enter the number of simulations you want to do: "); 
     int[] userInput =new int [2]; //my problem 
     userInput[0]= stdIn.nextInt(); //my problem 
     System.out.println("Please enter the size of the group you want : "); 
     int[] userInput1 = new int [2]; 
     userInput[1] = stdIn.nextInt(); 
     int a = userInput[1]; 
     while (a<2 || a>365) 
     { 
      System.out.println("please type the number that is between 2~365"); 
     } 

     System.out.println(); 

     return promptAndRead(stdIn); 
    } 


    // Method for calculations 
    public static double compute(int numOfSimulation, int numOfPeople) 
    { 
     for (int i =0; i < numOfPeople; i++) 
     { 
      Random rnd = new Random(1); 
      //Generates a random number between 0 and 364 exclusive 
      int num = rnd.nextInt(364); 
      System.out.println(num); 
      System.out.println(num/365 * numOfPeople * numOfSimulation); 
     } 
     return numOfPeople; 
    } 

} 
+2

'promptAndRead()'はどのように終了すると思いますか?無条件に自分自身を再帰的に呼び出すので、どの時点で停止するのでしょうか?つまり、最終的な 'StackOverflowError'以外のもの、あるいはユーザがあきらめてゴミを入れたときの' InputMismatchException'です。 ---もちろん、ユーザが 'a 'の値を1と入力しないと仮定すると、あなたの画面はメッセージ* adininum *であふれます。 ---質問: 'userInput1'は何のために使われますか? – Andreas

+2

なぜあなたはあなたの質問にJavaScriptとしてタグをつけましたか? –

+0

なぜ 'userInput'と' userInput1'はint配列ですか?なぜ単純な整数変数ではないのですか? – UnholySheep

答えて

0

見つけた!!!!!!!!!!!

は、次の操作を行います。

// User input prompt where you make the simulation. For people and return them as an array 
public static int[] promptAndRead(Scanner stdIn) 
{ 
    System.out.println("Please enter the number of simulations you want to do: "); 
    int[] userInput =new int [2]; //CHANGE THIS TO 1? 
    userInput[0]= stdIn.nextInt(); //my problem 
    System.out.println("Please enter the size of the group you want : "); 
    int[] userInput1 = new int [2]; //CHANGE THIS TO 1? 
    userInput[1] = stdIn.nextInt(); 
    int a = userInput[1]; 
    while (a<2 || a>365) 
    { 
     System.out.println("please type the number that is between 2~365"); 
    } 


    System.out.println(); 

    return(userInput); 

} 

が私を知ってみましょう配列を返すために!

+0

ありがとうございました!出来た! – FocusOnly

+0

@FocusOnlyここに投稿する前にプログラムのデバッグを試しましたか? –

0

私は実際にあなたがそのuserInputとそこにそれを行うことができるとは思わない、私はこれを言っていますこのプログラムを実行する方法は非常に難解ですから。

あなたは、あなたのような変更されますどのようなものにそれを変更する可能性がある場合、私は疑問に思う、プロンプトで2つの配列を呼び出している:

// User input prompt where you make the simulation. For people and return them as an array 
public static int[] promptAndRead(Scanner stdIn) 
{ 
    System.out.println("Please enter the number of simulations you want to do: "); 
    int[] userInput =new int [2]; //CHANGE THIS TO 1? 
    userInput[0]= stdIn.nextInt(); //my problem 
    System.out.println("Please enter the size of the group you want : "); 
    int[] userInput1 = new int [2]; //CHANGE THIS TO 1? 
    userInput[1] = stdIn.nextInt(); 
    int a = userInput[1]; 
    while (a<2 || a>365) 
    { 
     System.out.println("please type the number that is between 2~365"); 
    } 


    System.out.println(); 

    return promptAndRead(stdIn); 

} 

としてもreturn promptAndRead(stdIn);が問題

ドンの一部である可能性があります)マルコフで提案を投げているだけでは分かりません;)