1,105,406 Community Members

Lotto - User input & array

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi all I have an assigment but I'm stuck on the loop. I've checked other threads here but got a lot of complicated code that I haven't study yet (i.e.: "System . out .print" is still Screen . message("a") for me)
So I have 6 lotto numbers 1,2,3,4,5
And I need to compare what the user inputs with above values.
if 1 value matchs " no winner"
if 2 values match " no winner"
if 3 values match " winner 3 numbers"
if 4 values match " winner 4 numbers"
if 5 values match " winner 5 numbers"
if 6 values match "Jackpot"

It does not to need if inputted values are repeated....
Can anyone help me? Thanks

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

you're stuck on the loop...

what loop? you haven't got a single line of code, let alone a block of code with a loop in it.

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

[Making sure I understand]
You need to create a lotto program where the user tries to match a 6-digit number (or string) such as "123456".

If the user matches at least three digits (in any order) in the string there is a winning message.
If the user matches all 6 digits, a jackpot message is displayed.

[Repeating numbers]
Are you saying that:
1) Repeating numbers are to be ignored
or
2) Repeating numbers are not allowed?

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Thines, You got the first part right, there is no need to check if user is inputting the same digit 2 times as we assume user is smart enough :-)

I'm creating an array but I can't exit once I type the 6 numbers... let alone comparing them. I know what I have to do I just can't figure out a way to put it together.

class lotto{
    public static void main(String[]args){
        int f[] = new int[6];
        int k[] = {1,2,3,4,5,6};

        for(int j=0; j<f.length; j++){
           System.out.print("Input you 6 lotto numbers: ");
           f = Keyboard.readInt();
           for(int i=0; i<f.length; j++){
                if(//not sure what to add here ){
                    System.out.print("winner");
                }
                else{
                    System.out.print(" not a winner");
                }
            }
         }
    }
}

Sorry, I was given several lego puzzles, just can't fit them together :-(
Any help is greatly appreciated!

Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

for(int i=0; i<f.length; j++){

replace j++ by i++

since you're never changing the value of i, it 'll never be > f.length :)

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi stultuske,
I've swapped the j for i as above
But I'm getting an error:

class lotto{
	public static void main(String[]args){
		int f[] = new int[6];
		int k[] = {1,2,3,4,5,6};

		for(int j=0; j<f.length; j++){
		System.out.print("Input you 6 lotto numbers: ");
		f = Keyboard.readInt();
		for(int i=0; i<f.length; i++){
			if(k==f){
				System.out.print("winner");
			}
			else{
				System.out.print(" not a winner");
			}
			}
		}
	}
}

Jcreator is giving me an error on line 8 :
f = Keyboard.readInt();
incompatible types Line 8

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

f is an array, so you will need to read them individually.

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi Thines.
Thanks for you help
so I've done this:

class lotto{
	public static void main(String[]args){
		int f[] = new int[6];
		int k[] = {1,2,3,4,5,6};

		for(int j=0; j<f.length; j++){
			System.out.print("Input you 1st lotto number: ");
			f[0] = Keyboard.readInt();
			System.out.print("Input you 2nd lotto number: ");
			f[1] = Keyboard.readInt();
			System.out.print("Input you 3th lotto number: ");
			f[2] = Keyboard.readInt();
			System.out.print("Input you 4th lotto number: ");
			f[3] = Keyboard.readInt();
			System.out.print("Input you 5th lotto number: ");
			f[4] = Keyboard.readInt();
			System.out.print("Input you 6th lotto number: ");
			f[5] = Keyboard.readInt();

			for(int i=0; i<f.length; i++){
				if(k==f){
					System.out.print("winner");
				}
				else{
					System.out.print(" not a winner");
				}
			}
		}
	}
}

But it accepts input but if I type all the correct numbers {1,2,3,4,5,6} Is not doing the if/else statement and loop is not closing.

i.e:
Input you 1st lotto number: 1
Input you 2nd lotto number: 2
Input you 3th lotto number: 3
Input you 4th lotto number: 4
Input you 5th lotto number: 5
Input you 6th lotto number: 6
not a winner not a winner not a winner not a winner not a winner not a winnerInput you 1st lotto number:
--- End---
Any idea how can I resolved the loop and if/else?
Thanks

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

You must decide if you want the numbers all at once or one at a time.

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I'd say of I put them numbers one at the time will help me with the if/else statement as my original problem would demand check for amount of winning numbers...

Makes sense, let me see if I can figure it out.

I think u know the solutin for above, but I like your approach, this is exactly what I needed, some input to help me make sense of this.
Thanks,
I'll be back in a while, will try and work someing out :-P

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

You'll probably end up with a lot more code than you think.
You will need to compare the two arrays to see if they match.
You will need to compare one array against itself to make sure there are no repeating numbers (in my world).
You will also need to set a guage to determine what is (and is not) winning.

I also like to use descriptive variable names, so I don't forget what they are later on and so I never have to guess their type.

like:

//////////////////////////////////////////////////////////////////////////
      // Repeating numbers don't make sense and will not be tolerated ;)
      if(hasRepeatingNums(arr_intUserGuess))
      {
         System.out.println("That entry has repeating numbers and is invalid");
         return;
      }

...where I can tell the question being asked is if there are repeating numbers in an array of integers that are what the user guessed.

Of course, you'd need to make the hasRepeatingNums function.

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Unfortunately I had about 8 clases of java, and most of the things I've learn so far would be very restrictive in what to add.

I was able to integrate the values inputted all in one by changing
{code}
k[j] =Keyboard.Int();
{/code]
The loop is still not closing, I've try to change the loop positioning in the code and got an error. So I just reach a dead end.
As I said previously, I was given all the puzzle pieces ( array, if/else statements) but I was not given the instructions in how to put it all together...
Will leave it for the day! have a headche!
Thanks :-)

Member Avatar
thines01
Postaholic
2,420 posts since Oct 2009
Reputation Points: 389 [?]
Q&As Helped to Solve: 413 [?]
Skill Endorsements: 10 [?]
Team Colleague
Featured
 
0
 

So, keep in mind: if you know how to make functions, do so.
Each different element of this could/should be broken out into a different function -- that way, you can
1) keep your project straight and manageable
2) be able to REUSE solutions for problems you've already solved
3) avoid contaminating proven solutions by accident or bad design

With that said, think about how you would use something like this:

/////////////////////////////////////////////////////////////////////////////
   // Returns the number of matches in two arrays of integers
   // Duplicates will skew the results, so use arrays with no duplicates.
   private static int getNumMatches(int[] a, int[] b)
   {
      int intNumMatches = 0;

      for(int i=0; i < b.length; i++)
      {
         for(int j=0; j < a.length; j++)
         {
            if(b[i] == a[j])
            {
               intNumMatches++;
            }
         }
      }

      return intNumMatches;
   }

Even if you don't immediately understand what it's doing, you can tell what it does.

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

So i found what I was looking for:

class lotto{
    public static void main(String[]args){
        int freq = 0;
        int lotto[] = {1,2,3,4,5,6};
        int user [] = new int[6]; 
        Screen.message("Input your 6 Lotto numbers: ");
        Screen.newline();
        for(int i=0; i<user.length; i++){
            user[i] = Keyboard.readInt(); 
        }

        for(int j=0; j<lotto.length; j++){
            for(int i=0; i<user.length; i++){
                if(lotto[j]==user[i]){
                    freq = freq + 1;
                }
                else{
                    ;
                }
            }
        }
        if(freq>=0 &&freq<=6){
                if(freq>= 0 && freq <= 2){
                      Screen.message("Sorry - No matches - Not a winner");  
                }
               else if(freq==3){
                      Screen.message("Congrats –3 winning numbers");  
                }
                else if(freq==4){
                      Screen.message("Congrats –4 winning numbers");  
                }
                else if(freq==5){
                      Screen.message("Congrats –5 winning numbers");  
                }
                else if(freq==6){
                      Screen.message("Congrats –6 winning numbers");  
                }

        Screen.newline();
    }
}
Member Avatar
stultuske
Posting Expert
5,326 posts since Jan 2007
Reputation Points: 938 [?]
Q&As Helped to Solve: 776 [?]
Skill Endorsements: 35 [?]
Featured
 
0
 

just a minor remark:
this check:

if(freq>= 0 && freq <= 2){

can be replaced by

if(freq <= 2){

you're just running a check you've already ran in your first if statement :)

Member Avatar
abvd
Newbie Poster
8 posts since Nov 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

True, Thanks :-)

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: