Results 1 to 12 of 12

Thread: Help with code.

  1. #1
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Help with code.

    I'm creating a simple and basic slot machine for a presentation. I'm trying to get it to work in Eclipse. I'm running to an issue with "String". Is there something else I can use instead to fix the issue.
    [highlight=Java]
    import java.util.Random;
    import java.util.Scanner;


    public class SM {

    public static void main (String[]args)
    {

    //Instance Data//

    int slot1;
    int slot2;
    int slot3;
    String start = "1";
    String end = "0";
    int play;

    //New Scanner and Random Generator//

    Scanner scan = new Scanner(System.in);

    Random generator = new Random();

    System.out.println("Enter 1 to start or 0 to stop playing.");

    play = scan.nextInt();

    while (play == 1)
    {
    //Number Generators//

    slot1 = generator.nextInt(10);
    slot2 = generator.nextInt(10);
    slot3 = generator.nextInt(10);

    //Determines if the numbers match and prints the appropriate response//

    if (slot1 == slot2 && slot2 == slot3)
    {
    System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Congratulations, You have won!");
    }

    if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
    {
    System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Close but not the right match.");
    }
    else
    {
    System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " None of the numbers match");
    }

    //Asks if player would like to play again//

    System.out.println("If you want to play again, Enter 1 for yes or 0 for no.");
    {
    play = scan.nextInt();
    }
    }
    if (play == 0)
    {
    System.out.println("Thank you for playing");
    }
    }
    }
    [/highlight]

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Help with code.

    an issue with "String". Is there something else I can use instead to fix the issue.
    Can you explain the "issue"? Please post the full text of any error messages.

    Please edit your post and wrap the code in code tags. See: BB Code List - Java Programming Forum
    If you don't understand my response, don't ignore it, ask a question.

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

    Default Re: Help with code.

    And go through the Forum Rules -- particularly the third paragraph.

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

  4. #4
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Help with code.

    Not understood your issue'
    '
    But this bug was noticed....'
    '
    In ur if else block...............'
    if (slot1 == slot2 && slot2 == slot3)
    {
    System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Congratulations, You have won!");
    }

    if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
    {
    System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Close but not the right match.");
    }
    else
    {
    System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " None of the numbers match");
    }
    '
    Even if you win, all same numbers,
    Both the statements are executed.

    '
    for 2nd 'if' block, write

    else if (slot1 == slot2 || slot1 == slot3 || slot2 == slot3)
    {
    System.out.println("Results are: " + slot1 + " " + slot2 + " " + slot3 + " Close but not the right match.");
    }

  5. #5
    suyog53 is offline Member
    Join Date
    Sep 2012
    Location
    Navi Mumbai
    Posts
    30
    Rep Power
    0

    Default Re: Help with code.

    --------------------------------------------------------------------------------
    Your issue with String...'
    --------------------------------------------------------------------------------

    In your program, you have just declared the variables 'start' and 'end'
    But not used anywhere'

    int slot1;
    int slot2;
    int slot3;
    String start = "1";
    String end = "0";
    int play;
    Just try commenting them.....'

    int slot1;
    int slot2;
    int slot3;
    //String start = "1";
    //String end = "0";
    int play;
    The program runs as desired correctly......................'
    '

  6. #6
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help with code.

    I apologize for the late response I figured out the problem and solved any issue I had.

  7. #7
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help with code.

    Can a Moderator please delete this post.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with code.

    Quote Originally Posted by LostinThoughts View Post
    Can a Moderator please delete this post.
    Why would you want it deleted? Probably best to leave it open and for you to post your solution in case future programming students run into a similar problem and find this site when searching for a solution.


    I apologize for the late response I figured out the problem and solved any issue I had.
    Did you look at the answers and did they help you solve your problem? Consider thanking the folks who put in the effort to help you since they're all volunteers and helped on their free time. I'm sorry about being a nudge, but this is how we're paid, and it really isn't asking a lot, I don't think.
    Last edited by Fubarable; 09-30-2012 at 03:05 AM.

  9. #9
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help with code.

    Yes, I looked at the answers to resolve my problem. I would like to have it closed that's all. I do not even know how to thank them and have no problem thanking them at all.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help with code.

    Quote Originally Posted by LostinThoughts View Post
    Yes, I looked at the answers to resolve my problem. I would like to have it closed that's all. I do not even know how to thank them and have no problem thanking them at all.
    That will do, thanks!

    But again, out of curiosity, what did you do to solve it?

  11. #11
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help with code.

    I will like to thank suyog53 and mention that it's solved.

  12. #12
    Join Date
    Sep 2012
    Posts
    11
    Rep Power
    0

    Default Re: Help with code.

    I actually just rewrote it line by line and noticed the same mistake.

Similar Threads

  1. Replies: 16
    Last Post: 01-31-2012, 09:36 PM
  2. Replies: 3
    Last Post: 08-10-2011, 10:17 AM
  3. Replies: 0
    Last Post: 08-07-2011, 09:32 PM
  4. Replies: 0
    Last Post: 02-21-2011, 12:50 PM
  5. Replies: 1
    Last Post: 04-26-2007, 04:52 PM

Posting Permissions

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