1. Home
  2. command
  3. Java Random Class Question?

Java Random Class Question?

By WinPie Posted in: command

I have two classes service and application class.
So far my program can generate 5 random numbers, but now i am trying to enter my 5 numbers so that, later on i can compare those number and find matches. But i am stuck, i can not enter number in my application class.
this is what i have so far,
import java.util.Random;
public class Lottery
{
private int[] num = new int[5];
public Lottery()
{
Random random = new Random();
for(int i = 0; i < num.length; i++){
num[i] = random.nextInt(51);
}
}
public String toString()
{
String s = "";
for(int i = 0; i < this.num.length; i++)
s += this.num[i] + " ";
s += "\n";
return s;
}
}
————————————–… (next application class)
import java.util.Scanner;
public class Lab1
{
public static void main(String [] args)
{
Lottery lot = new Lottery();
System.out.println("lottery number " + myNumbers(i + 1));
System.out.println("Lottery Numbers: \n" + lot.toString());
}
public int[] myNumbers() {
Scanner keyboard = new Scanner(System.in);
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
numbers[i] = keyboard.nextInt();
}
return numbers;
}
}
please help, thanks

Get Chitika Premium
  1. nobody no one Says

    in Lab1 class:
    System.out.println(“lottery number ” + myNumbers(i + 1)); <—– the variable i is undefined?! what is that???
    + the method myNumbers doesn’t take any arguments, why are you trying to send (i+1) ???
    EDIT: is this what you are looking for?
    import java.util.Scanner;
    public class Lab1 {
    public static void main(String[] args) {
    Lottery lot = new Lottery();
    //Save the array before you call elements from it!
    int[] numbers = myNumbers();
    System.out.println(“Your Guesses:”);
    for (int i = 0; i < numbers.length; i++)
    System.out.print(numbers[i]+” “);
    System.out.println(“nLottery Numbers: n” + lot.toString());
    }
    //static method!!!
    public STATIC int[] myNumbers() {
    Scanner keyboard = new Scanner(System.in);
    int[] numbers = new int[5];
    for (int i = 0; i < numbers.length; i++) {
    numbers[i] = keyboard.nextInt();
    }
    return numbers;
    }
    }

Leave a Reply

You must be logged in to post a comment.

More Interesting Things

©2011 Windows Pie, All rights reserved.