Results 1 to 16 of 16
Like Tree3Likes
  • 1 Post By JosAH
  • 1 Post By corong1997
  • 1 Post By jim829

Thread: Help with assignment?

  1. #1
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default 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);
    }
    
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Help with assignment?

    Quote Originally Posted by corong1997 View Post
    I'm getting some issues with my if and else statements. Could somebody tell me what's wrong?
    Not without a crystal ball, no. You need to tell us what those 'issues' are.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default 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;
            }
        }
    the gradea = 0 pulls up an error, saying "cannot find symbol - gradea". I'm sure there is a super simple fix to this, but I just started and have no idea what is wrong and how to fix it.

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default 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;
        }
    }
    First, your 'else if' conditional has a semi-colon right after it. So what you probably want is the following:
    Java Code:
    {
    
        if (x == c)
        {
        int gradea = 1;
        }
        else if (x != c)
        {
        gradea = 0;
        }
    }
    Having said that, you don't need the else if conditional, because if x == c, the only other possibility is that it does not equal c. So do the following:
    Java Code:
    {
    
        if (x == c) // if x == c
        {
        int gradea = 1;
        }
        else // x != c
        {
        gradea = 0;
        }
    }
    Finally, as had already been stated, change the scope of your int declaration.
    Java Code:
    int gradea = 0;
    {
        if (x == c)
        {
            gradea = 1;
        }
    }
    Now gradea has a default value of 0. So all you need to do is test if x == c to change it.

    This does not ensure that the rest of your code is error free or that it will do as you desire.

    Regards,
    Jim
    Last 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

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default Re: Help with assignment?

    Quote Originally Posted by jim829 View Post
    Note: The following is syntactically wrong.
    .s/syntac/seman/

    kind regards,

    Jos
    DarrylBurke likes this.
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help with assignment?

    Oh okay, that makes sense. Thanks everybody!

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default Re: Help with assignment?

    Well duh!! Otherwise, it wouldn't even compile.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default 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");
                }
            }

  10. #10
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default 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

  11. #11
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default 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?

  12. #12
    Ubiquitous is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default Re: Help with assignment?

    Quote Originally Posted by corong1997 View Post
    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");
                }
            }
    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);
    As you can see you are already storing them but not until after you already display information that gets thrown out. Think of storing data then displaying.

    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);

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,105
    Rep Power
    6

    Default Re: Help with assignment?

    Quote Originally Posted by corong1997 View Post
    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?
    I have little to add to what Ubiquitous said. Only that you prove just one thing here: you can't get it done by just trying stuff and hoping for the best, you have to understand it first.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    corong1997 is offline Member
    Join Date
    Sep 2013
    Posts
    10
    Rep Power
    0

    Default 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
    DarrylBurke likes this.

  15. #15
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

    Default Re: Help with assignment?

    Right. But before you start your next thread, go through the Forum Rules, especially the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  16. #16
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,786
    Rep Power
    5

    Default 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,
    Jim
    DarrylBurke likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Please help me with my assignment
    By Shin in forum New To Java
    Replies: 3
    Last Post: 10-15-2012, 11:14 PM
  2. HW assignment help
    By mackavelirip in forum New To Java
    Replies: 5
    Last Post: 03-31-2011, 04:34 AM
  3. assignment help
    By xyknight in forum New To Java
    Replies: 6
    Last Post: 03-13-2011, 08:19 PM
  4. help with an assignment!
    By Tek in forum New To Java
    Replies: 6
    Last Post: 10-25-2010, 12:32 AM
  5. Need help with assignment! please
    By runawaykinms in forum Java Applets
    Replies: 2
    Last Post: 10-06-2010, 10:58 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •