Results 1 to 16 of 16
Thread: Help with assignment?
- 09-01-2013, 12:19 AM #1
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Help with assignment?
Hi everybody. I just started learning java in my computer science class, and I have to write a program for a lottery. Here is my current code, but I'm getting some issues with my if and else statements. Could somebody tell me what's wrong?
Java Code:import java.util.Random; public class Lottery { public static void main(String[] args) { Random gen = new Random(); System.out.println("Your numbers are:"); System.out.println(gen.nextInt(100)); int x = gen.nextInt(100); System.out.println(gen.nextInt(100)); int y = gen.nextInt(100); System.out.println(gen.nextInt(100)); int z = gen.nextInt(100); System.out.println(gen.nextInt(100)); int a = gen.nextInt(100); System.out.println(gen.nextInt(100)); int b = gen.nextInt(100); System.out.println("The winning numbers"); System.out.println(gen.nextInt(100)); int c = gen.nextInt(100); System.out.println(gen.nextInt(100)); int d = gen.nextInt(100); System.out.println(gen.nextInt(100)); int e = gen.nextInt(100); System.out.println(gen.nextInt(100)); int f = gen.nextInt(100); System.out.println(gen.nextInt(100)); int g = gen.nextInt(100); { if (x == c) { int gradea = 1; } else if (x != c); { gradea = 0; } } { if (y == d) { int gradeb = 1; } else if (y != d); { gradeb = 0; } } { if (z == e) { int gradec = 1; } else if (z != e); { gradec = 0; } } { if (a == f) { int graded = 1; } else if (a != f); { graded = 0; } } { if (b == g) { int gradee = 1; } else if (b != g); { gradee = 0; } } System.out.println(gradea + gradeb + gradec + graded + gradee); } }
- 09-01-2013, 07:42 AM #2
- 09-01-2013, 08:52 AM #3
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Re: Help with assignment?
The long chunk of System.out.println is fine. Then, when I have my if statements, I try to store the result as an integer, but I get an error next time I try to use it. So for example, when I have
Java Code:{ if (x == c) { int gradea = 1; } else if (x != c); { gradea = 0; } }
- 09-01-2013, 09:01 AM #4
Re: Help with assignment?
Have you learned about the scope of variables? A variable is visible only in the scope in which it is declared. Which commonly means the immediately surrounding pair of braces { ... }
To use a variable in both an if and an else statement, that variable needs to be declared outside of both.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 09-01-2013, 07:11 PM #5
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Help with assignment?
Note: The following is syntactically wrong. Here is a progression of changes that gradually improve the logic.
Java Code:{ if (x == c) { int gradea = 1; } else if (x != c); { gradea = 0; } }
Java Code:{ if (x == c) { int gradea = 1; } else if (x != c) { gradea = 0; } }
Java Code:{ if (x == c) // if x == c { int gradea = 1; } else // x != c { gradea = 0; } }
Java Code:int gradea = 0; { if (x == c) { gradea = 1; } }
This does not ensure that the rest of your code is error free or that it will do as you desire.
Regards,
JimLast edited by jim829; 09-01-2013 at 07:32 PM.
The JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-01-2013, 07:50 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
- 09-01-2013, 08:31 PM #7
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Re: Help with assignment?
Oh okay, that makes sense. Thanks everybody!
- 09-01-2013, 09:45 PM #8
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Help with assignment?
Well duh!! Otherwise, it wouldn't even compile.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
- 09-02-2013, 10:14 AM #9
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Re: Help with assignment?
Okay, so I got the int and if statements to work, and I created an input feature in JOption. I'm only running into one bug, and once I fix this, my program will be working great. Basically, sometimes when I input, for example, five 1's, and there are no 1's in the winning numbers, the program will still come up with one or two numbers correct, even though that's not what's showing up in the JOptionPane window. Here is the code. I suspect it has something to do with the lines after the "JOptionPane.showMessageDialog(null, "The winning numbers are");" line, but I'm not totally sure. You guys have all been a huge help, thanks so much!
Java Code:import java.util.Random; import javax.swing.JOptionPane; public class Lottery { public static void main(String[] args) { Random gen = new Random(); int x = Integer.parseInt(JOptionPane.showInputDialog(null, "Pick your first number")); int y = Integer.parseInt(JOptionPane.showInputDialog(null, "Pick your second number")); int z = Integer.parseInt(JOptionPane.showInputDialog(null, "Pick your third number")); int a = Integer.parseInt(JOptionPane.showInputDialog(null, "Pick your fourth number")); int b = Integer.parseInt(JOptionPane.showInputDialog(null, "Pick your fifth number")); JOptionPane.showMessageDialog(null, "The winning numbers are"); JOptionPane.showMessageDialog(null, gen.nextInt(10)); int c = gen.nextInt(10); JOptionPane.showMessageDialog(null, gen.nextInt(10)); int d = gen.nextInt(10); JOptionPane.showMessageDialog(null, gen.nextInt(10)); int e = gen.nextInt(10); JOptionPane.showMessageDialog(null, gen.nextInt(10)); int f = gen.nextInt(10); JOptionPane.showMessageDialog(null, gen.nextInt(10)); int g = gen.nextInt(10); int gradea = 0; if (x == c) gradea = 1; int gradeb = 0; if (y == d) gradeb = 1; int gradec = 0; if (z == e) gradec = 1; int graded = 0; if (a == f) graded = 1; int gradee = 0; if (b == g) gradee = 1; int score = gradea + gradeb + gradec + graded + gradee; JOptionPane.showMessageDialog(null, "You got " + score + "numbers correct"); } }
- 09-02-2013, 10:19 AM #10
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Help with assignment?
You realize that when you display the random number, you are GENERATING one which you then throw away, and then you generate another one which you assign to the cdefg variables. So what you are seeing in the dialog is not the random number you are actually using.
Last edited by gimbal2; 09-02-2013 at 11:22 AM. Reason: typo
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 09-02-2013, 10:42 AM #11
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Re: Help with assignment?
Oh, okay that makes a lot of sense. I tried doing an Integer.parseInt like I did when inputting the numbers, but I got an error "'void' type not allowed here". What would be a possible way to generate a number and then store it as an integer?
- 09-02-2013, 11:16 AM #12
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Re: Help with assignment?
In your code you are already storing the values you are just doing it in a slightly incorrect order.
What your code is doing
Java Code:// This line is displaying a message box which displays a randomly generated number. // Since this number is not stored it is thrown away once you close the message box. JOptionPane.showMessageDialog(null, gen.nextInt(10)); // In this line you are creating an int called 'c' which stores a new randomly generated integer. // The odds of it being the same as the displayed integer in the previous line is low. int c = gen.nextInt(10);
For example.
Java Code:// Create scanner object to get user input Scanner usrInput = new Scanner(System.in); // Ask user name that can be store into a variable System.out.print("Hello what is your name? "); // Store user's input into variable called name String name = usrInput.nextLine(); // Display user's name. System.out.println("Hey! My name is also " + name);
- 09-02-2013, 11:36 AM #13
Just a guy
- Join Date
- Jun 2013
- Location
- Netherlands
- Posts
- 5,114
- Rep Power
- 13
Re: Help with assignment?
"Syntactic sugar causes cancer of the semicolon." -- Alan Perlis
- 09-02-2013, 12:08 PM #14
Member
- Join Date
- Sep 2013
- Posts
- 10
- Rep Power
- 0
Re: Help with assignment?
Ah, okay. That makes a lot sense how I was trying to do it backwards rather than store first. Thanks! And gimbal- Learning to program has definitely begun to teach me that. It's really humbling to come onto a forum like this and have so many people willing to help me out, even though the errors I run into are super simple. You guys are awesome
- 09-02-2013, 12:12 PM #15
Re: Help with assignment?
Right. But before you start your next thread, go through the Forum Rules, especially the third paragraph.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 09-02-2013, 04:13 PM #16
Senior Member
- Join Date
- Jan 2013
- Location
- Northern Virginia, United States
- Posts
- 6,226
- Rep Power
- 15
Re: Help with assignment?
It seems to me that in your version of the lottery, order is important, not just choosing the correct numbers. For example, you check
if x == c and
y == d.
What if x = 5 and c = 6
and y = 6 and d = 5?
Did they choose 2 correct or 0 correct? If this is for an assignment, you had better make certain.
Regards,
JimThe JavaTM Tutorials | SSCCE | Java Naming Conventions
Poor planning on your part does not constitute an emergency on my part
Similar Threads
-
Please help me with my assignment
By Shin in forum New To JavaReplies: 3Last Post: 10-15-2012, 11:14 PM -
HW assignment help
By mackavelirip in forum New To JavaReplies: 5Last Post: 03-31-2011, 04:34 AM -
assignment help
By xyknight in forum New To JavaReplies: 6Last Post: 03-13-2011, 08:19 PM -
help with an assignment!
By Tek in forum New To JavaReplies: 6Last Post: 10-25-2010, 12:32 AM -
Need help with assignment! please
By runawaykinms in forum Java AppletsReplies: 2Last Post: 10-06-2010, 10:58 AM
Bookmarks