• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Need help with my Lottery Program

 
Isaac Davis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what's wrong with it, I don't get any compiling errors and it runs but, but not correctly.

This is my Code, any help/tips for future programs would be appreciated.



 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I corrected your code tags to get it to display better.
[edit]apparently there was a minor bug in our release that cause the code tags to not work correctly.[\edit]

The first tip i have is to ask you to explain what "it runs but, but not correctly" means. What do you expect it to do, and what does it do? Why is that not what you expect? We have no way to know how to 'fix' the code to get it to do what you expect if we don't know what that is.

The easier you make it for someone to help you, the more likely you are to get that help.
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're using the "==" operator to compare strings - BIG no-no! You need to use the "equals" method instead.
 
Sean Clark
Rancher
Posts: 377
Android Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey,

My question would be, why are you changing all your int values to String objects?

Sean

Oops, I see why you have used it, please ignore me.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think about your conditional logic. Hint: You have four conditions, the first two are "equal" and "not equal".
 
Isaac Davis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fred rosenberger wrote:I corrected your code tags to get it to display better.
[edit]apparently there was a minor bug in our release that cause the code tags to not work correctly.[\edit]

The first tip i have is to ask you to explain what "it runs but, but not correctly" means. What do you expect it to do, and what does it do? Why is that not what you expect? We have no way to know how to 'fix' the code to get it to do what you expect if we don't know what that is.

The easier you make it for someone to help you, the more likely you are to get that help.


Right now it's set up to create a random number between 100 and 999 I have it set up so that it shows what that number is so I can test it, when i test the program it doesn't run correctly because let's say the number is 600, if I put in 600 it doesn't say the user has won, this
is supposed to make it do that. The other two wins are supposed to be triggered if the user's inputted number has the same first two numbers or last two numbers as the number randomly selected. Hope that explains that well enough if not I don't mind trying to explain it more specifically again. Thanks for the help, sorry for not being specific enough.


Paul Beckett wrote: think about your conditional logic. Hint: You have four conditions, the first two are "equal" and "not equal".

Not sure what you mean, my first condition (the for loop) runs the loops if the remaining tries the user has doesn't equal 5. and the second compares the number the user puts in to the random number, and then if it's not equal it prompts the user for a new guess(entry).

Sean Clark wrote: Oops, I see why you have used it, please ignore me.

Don't sweat it , I appreciate you trying to help.

Lester Burnham wrote: You're using the "==" operator to compare strings - BIG no-no! You need to use the "equals" method instead.

I wasn't aware that the == couldn't be used for strings as well, i thought it was just used for anything within a boolean statement (if,for,while etc.), it's worked fine for me before but I don't think I was comparing strings then, thanks for the tip. I've changed that but I'm still getting the same results, it's only printing the first if statement that prompts the user for a new entry.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Beckett wrote:Think about your conditional logic. Hint: You have four conditions, the first two are "equal" and "not equal".

I was trying to give you a nod in the right direction without giving you the full answer so I'll rephrase (from your original code, and assuming "==" is going to work correctly):
Condition 1: myEntry!=myRandom
Condition 2: myEntry==myRandom
Condition 3: myEntry.substring(1,2) == myRandom.substring(1,2)
Condition 4: myEntry.substring(0,1) == myRandom.substring(0,1)

With myEntry=123, myRandom=123 then condition 2 is executed //equal
myEntry=123,myRandom=456 then condition 1 is executed //not equal
myEntry=123,myRandom=124 then condition 1 is executed //not equal (first 2 numbers same)
myEntry=123,myRandom=923 then condition 1 is executed //not equal (second 2 numbers same)
So as you can see, all possible results are covered by conditions 1 and 2. Conditions 3 and 4 cannot be reached. Make sense now?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49843
70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Beckett wrote:. . . assuming "==" is going to work correctly. . . .
I don't think that would make any difference; a == b || a != b is always a tautology, so cases 3 and 4 could never be reached.
 
Isaac Davis
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Beckett wrote:Make sense now?

Yeah I get it now, I guess I had just figured that would be ignored but now I see what you're saying. I really appreciate your help and everyone elses, thanks a ton folks. I'm pretty sure I should be able to fix it now, just gotta work through the rest of it. Oh and thanks for the new vocab word(Tautology) Ritchie.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic