Win a copy of Spark in Action this week in the Open Source Projects forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Bear Bibeault
• Ron McLeod
• Jeanne Boyarsky
• Paul Clapham
Sheriffs:
• Tim Cooke
• Liutauras Vilda
• Junilu Lacar
Saloon Keepers:
• Tim Moores
• Stephan van Hulst
• Tim Holloway
• fred rosenberger
• salvin francis
Bartenders:
• Piet Souris
• Frits Walraven
• Carey Brown

# 3 digit lottery case

Ranch Hand
Posts: 45
Hi everyone,

I have got a task to make the lottery system where user is asked input three digit integer if he wants to guess the randomly generated numbers.
Currently I am at the barrier in the whole evaluation system. Got the first part right where exact order needs to be checked for the main price, however
the next two awards are pain in the a** ; Could you please help me out with the idea behind comparing the digits for this specific grading system?

thank you very much

Problem:

Code:

Greenhorn
Posts: 14
I'm not sure if this is for a class or if you're learning on your own but one thing you might want to look into are collections/arrays. If this is for a class then you'll be covering arrays and loops in the near future (I assume) and are expected to answer this problem without the use of either loops or arrays?

You could just check each value against each other value and keep a track of how many matches you accumulate.

User Numbers = A B C
Lottery Numbers = D E F

The '||' is the "Or" operator. So the below statements say: "Increment the count of matches If A equals D OR A equals E OR A equals F".

If (A == D || A == E || A == F) { matchCount++};
If (B == D || B == E || B == F) { matchCount++};
if (C == D || C == E || C == F) { matchCount++};

Then you'll know how many matches you have. I think that makes sense.

Rancher
Posts: 3742
16

Greg Hatt wrote:The '||' is the "Or" operator. So the below statements say: "Increment the count of matches If A equals D OR A equals E OR A equals F".

If (A == D || A == E || A == F) { matchCount++};
If (B == D || B == E || B == F) { matchCount++};
if (C == D || C == E || C == F) { matchCount++};

Then you'll know how many matches you have. I think that makes sense.

The only problem with that is that there is no validation that all the entered numbers are different (or even that all the lottery numbers are different).
If the lottery numbers were 1, 2 and 3 and the user entered 333, then they would win.

Greg Hatt
Greenhorn
Posts: 14
True. I just sort of assumed that the lottery numbers themselves would have to be unique and the user's guess would have to be unique as well but after looking at the code I see it's not a business rule of this particular application.

Sorry for misleading OP. Keeping with above theme, or not using arrays or loops and if all numbers can be the same then you'll end up writing conditional statements for every possible combination of matches.

Greenhorn
Posts: 20
Hey all,
You can try this approach for better work,

Instead of working with integers its better to work with Strings for this type of iterative work,

Reply if it helps

Joanne Neal
Rancher
Posts: 3742
16

Greg Hatt wrote:Sorry for misleading OP.

I don't think you were misleading him. You pointed him/her in the right direction which is what this forum is all about. I was just adding some extra directions for when he got to the point you had directed him to

Lovro Bajc
Ranch Hand
Posts: 45

I'm not sure if this is for a class or if you're learning on your own but one thing you might want to look into are collections/arrays. If this is for a class then you'll be covering arrays and loops
in the near future (I assume) and are expected to answer this problem without the use of either loops or arrays?

I am going through the book on my own, and as far as my understanding goes, arrays and loops are coming up shortly It is just an exercise in the book Introduction to Java Programming Comprehensive Version 10th edition. under section 3 (boolean, if, switch) .

But before to do it with collections/arrays I need to improvise i guess so now what I have come up with is bellow, and I think it should work (At least when I was testing few numbers). I also changed the major price to a shorter version :

thanks for all the help

 We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad: Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
Similar Threads