Java数组和nextInt();不能正常工作

时间:2016-09-18 19:42:11

标签: java arrays random

大家好,我遇到了一个数组问题而.nextInt();这导致我的输出线在第3个提示符处向上移动而不是在下面,并且严重无法弄清楚是什么错误。< / p>

我试过了.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 个答案:

答案 0 :(得分: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);也可能是问题的一部分

不知道马可夫只是提出建议;)

答案 1 :(得分: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);

}

返回数组

让我知道!