dimanche 31 juillet 2016

Generating Random Numbers in For Loop from External Class

First of all, I apologize if this has been asked before and I couldn't find it. I scoured the depths of the internet looking for some guidance but did not come up successful.

In my programming II class we are working on creating a program for the lottery. We have created external classes (did that in the classroom) and have used the objects created in those classes in our driver. For this assignment, we are creating a program which outputs lottery numbers. The program asks the user if they are playing Pick 3, 4, or 5 and then is supposed to output a random number for each "ball". In one of the separate classes we have already imported and instantiated the randomizer. However, when I program the array and for loop, while it generates the proper number of printouts (3 printouts for 3, 4 printouts for 4, and 5 printouts for 5), a new number is not being generated for each loop. Instead, it is holding onto the first value and printing that out for each loop after. I've included my code below. Please help!

import java.util.Scanner;

public class LotteryGame2 {

  public static void main(String[] args){

    //Declare and instantiate objects
    PickGame2 pick = new PickGame2();
    Scanner keyboard=new Scanner(System.in);
    pick.activate();

    //Ask for and obtain user input
    System.out.print("Are you playing Pick 3, Pick 4, or Pick 5? Enter number here: ");
    int numberOfGame=keyboard.nextInt();
    PickGame2[] gamenumber=new PickGame2 [numberOfGame];

    for (int i=0; i<gamenumber.length; ++i){
        int ball=pick.pullBall();
        System.out.println("Ball " + (i+1) + " is " + ball);
    }//Ending bracket of for loop

    //Close Scanner object
    keyboard.close();

  }//Ending bracket of main method
}//Ending bracket of class LotteryGame2




Aucun commentaire:

Enregistrer un commentaire