• Post Reply Bookmark Topic Watch Topic
  • New Topic
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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

3 digit lottery case

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Suse Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Suse Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Humans and their filthy friendship brings nothing but trouble. My only solace is this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic