Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: homwork java lotto question help.

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default homwork java lotto question help.

    here is my question

    1. Write a Java program that randomly generates a five-digit lottery number and prompts the user to enter a five-digit number. Each digit in the number is in the range between 0~9. The program should determine which prize the user wins according to the following rule:
    The user wins the first prize if the user input matches all five digits in the lottery number in exact order.
    The user wins the second prize if the user input matches any four digits in the lottery number in exact positions.
    The user wins the third prize if the user input matches any three digits in the lottery number in its exact position.
    The user wins the fourth prize if the user input matches any two digits in the lottery number in its exact position.

    The user wins the fifth prize if the user input matches any one digit in the lottery number in its exact position.

    here is my code. I have tried replacing the && statements with || and it always returns either case 1 or case default.
    import java.util.Scanner;
    import java.util.Random;
    class Hw5 {
    static int getPrize(int g1, int g2, int g3, int g4, int g5,
    int u1, int u2, int u3, int u4, int u5) {
    //code for determining the prize comparing (g1, g2, g3, g4, g5) to (u1, u2, u3, u4, u5)
    if (u1 == g1 && u2 == g2 && u3 == g3 && u4 == g4 && u5 == g5)
    return 1;
    else if (u1 == g1 || u2 == g2 || u3 == g3 || u4 == g4 && u5 != g5)
    return 2;
    else if (u1 == g1 && u2 == g2 && u3 == g3)
    return 3;
    else if (u1 == g1 && u2 == g2)
    return 4;
    else if (u1 == g1)
    return 5;
    else
    return 0;
    }
    public static void main(String [] args) {
    Scanner in = new Scanner(System.in);
    Random rand = new Random();

    int g1, g2, g3, g4, g5; //5-digit number generated
    int u1, u2, u3, u4, u5; //5-digit number entered by user
    int prize;

    g1 = rand.nextInt(10); //generate an integer between 0 and 9
    g2 = rand.nextInt(10);
    g3 = rand.nextInt(10);
    g4 = rand.nextInt(10);
    g5 = rand.nextInt(10);
    System.out.print(g1);
    g1 = rand.nextInt(10);
    System.out.print(g2);
    g2= rand.nextInt(10);
    System.out.print(g3);
    g3= rand.nextInt(10);
    System.out.print(g4);
    g4= rand.nextInt(10);
    System.out.print(g5);
    g5= rand.nextInt(10);

    System.out.print("Enter the first digit(0-9):");
    u1 = in.nextInt();
    System.out.print("Enter the second digit(0-9):");
    u2 = in.nextInt();
    System.out.print("Enter the third digit(0-9):");
    u3 = in.nextInt();
    System.out.print("Enter the fourth digit(0-9):");
    u4 = in.nextInt();
    System.out.print("Enter the fifth digit(0-9):");
    u5 = in.nextInt();

    prize = getPrize(g1, g2, g3, g4, g5, u1, u2, u3, u4, u5);

    switch (prize) {
    case 1:
    System.out.println("You have won the first prize!");
    break;
    case 2:
    System.out.println("You have won the second prize!");
    break;
    case 3:
    System.out.println("You have won the third prize!");
    break;
    case 4:
    System.out.println("You have won the fourth prize!");
    break;
    case 5:
    System.out.println("You have won the fifth prize!");
    break;
    default:
    System.out.println("Sorry, you didn't win any prize!");
    break;
    }
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,640
    Thanks
    53
    Thanked 2,417 Times in 2,370 Posts

    Default Re: homwork java lotto question help.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    I have tried replacing the && statements with || and it always returns either case 1 or case default.
    What should it return?

    && means both must be true
    || means only one must be true
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    95
    Thanks
    3
    Thanked 14 Times in 14 Posts

    Default Re: homwork java lotto question help.

    try changing all the else if statements, to just if statements. That way nothing will get skipped over.

  4. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: homwork java lotto question help.

    Thank you that fixed it.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: homwork java lotto question help.

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    You forgot to ask a question or even describe what you needed help with. If the results you get are other than desired, please show actual results and what you think they should be and why. Posting sample runs should help you illustrate your point.

Similar Threads

  1. Java Servlet Lotto Application
    By princessfi92 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 8th, 2012, 05:33 PM
  2. Lotto Java Servlet
    By princessfi92 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 8th, 2012, 04:53 PM
  3. lotto ticket generator help
    By tlckl3m3elmo in forum What's Wrong With My Code?
    Replies: 19
    Last Post: July 15th, 2011, 12:39 AM
  4. Loopy Lotto....
    By JavaCow in forum What's Wrong With My Code?
    Replies: 4
    Last Post: October 18th, 2010, 06:28 PM
  5. new program (lotto.class)
    By james137 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 3rd, 2009, 05:22 PM

Tags for this Thread