dimanche 27 novembre 2016

1D and 2D Arrays, random numbers in Java for a lottery game

I am trying to develop a lottery game in java, where the user enters 3 lines of 5 numbers and the application generates 5 random numbers, compares the guessed numbers and returns to the user the guessed numbers for each line.

I have tried to use a 2D array for the input from the user and 1D array for the 5 random numbers. But it is not working...I am getting a lot of errors and not sure how to make it work...

Below are the app and instantiable classes, anyone could help me?

INSTANTIABLE

import java.util.Random;
public class Lotto{


    Random myRand = new Random();

    //declare instance variables
    private int [] myArray;
    private int [][] guesses;
    private int result;


    for(int i=0; i<5; i++){
        myArray[i]= myRand.nextInt(40);
        myArray += 1;
    }

    public Lotto(){

    }
    public void setGuess(int [][]g){
        guesses=g;
    }
    public int getResult(){
        return result;
    }
    public void computeResult(){
        int score1 =0;
        int score2 = 0;
        int scoe3 =0;

        //travesse 2D array
        for (int row=0; row<rain.length; row++){
            for (int col=0; col<guesses[row].length; col++){
                if(myArray[0]==guesses[0][col]){
                    score1 += 1;
                }else if(myArray[1]==guesses[0][col]){
                    score1 += 1;
                }else if(myArray[2]==guesses[0][col]){
                    score1 += 1;
                }else if(myArray[3]==guesses[0][col]){
                    score1 += 1;
                }else(myArray[4]==guesses[0][col]){
                    score1 += 1;
                }


            }
        }
    }
}

APP CLASS

import java.util.Scanner;

public class LottoApp{
    public static void main(String args[]){
        Scanner in = new Scanner(System.in);

        //declare a 2D array
        int [][] myArray;
        //create the 2D array
        int tries=3;
        int numbers=5;

        myArray = new int[tries][numbers];

        for (int rows=0; rows<myArray.length; rows++){
            for(int col=0; col<myArray[rows].length; col++){
                int line = rows+1;
                System.out.println("Number for list "+line);
                myArray[rows][col] = in.nextInt();
            }
        }
        Lotto myObj = new Lotto();
        //give data from user
        myObj.setGuess(myArray);

        //processing
        myObj.computeResult();

        //output
        int r = myObj.getResult();
        System.out.println("result: "+r);
    }
}




Aucun commentaire:

Enregistrer un commentaire