mardi 6 novembre 2018

Trying to change this Java random int function into a non-repeating function and add parallel arrays to display strings

This java code works in that it produces the amount of random ints the user inputs and they are within the range. I want to make it so the random ints do not repeat within the set produced. I also want to implement the array arr so it displays the arr string when that number is 'drawn' from the rand ints.

`public class Scenario { public Scenario(String string) { }

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);

    int year;

    int amount;



    /*

     Array of ~30 scenarios with parallel array for ints, pick how many scenarios you want that season, 
     randomly get that many non-repeating ints that print as the scenario text string


    */





    System.out.print("Please enter which season these scenarios will be for: ");

    year = input.nextInt(); 

    System.out.println("OK, the following scenarios will be for the season: " + year);


    System.out.print("Please enter how many scenarios you would like to receive this season: ");

    amount = input.nextInt(); 


    System.out.print("OK, Here are your scenarios for the season: " + year);

    //everything above works perfectly. these are the arrays i want to be parallel with the rand ints.

    Scenario[] arr; 


    arr = new Scenario[3]; 


    arr[0] = new Scenario("scenario 1 is to draft a qb"); 

    arr[1] = new Scenario("scenario 2 is to draft a rb"); 

    arr[2] = new Scenario("scenario 3 is to draft a wr"); 



    for (int i = 0; i < amount; i++) {



        //for each loop, pick next int in random array of scenarios


        for ( i = 0; i < amount; i++) {
            System.out.println(getRandomNumberInRange(1, 10));
        }
    }




    }

    private static int getRandomNumberInRange(int min, int max) {

        if (min >= max) {
            throw new IllegalArgumentException("max must be greater than min");
        }

        Random r = new Random();
        return r.nextInt((max - min) + 1) + min;
    }

}`




Aucun commentaire:

Enregistrer un commentaire