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 8 of 8

Thread: Help! Help! Help!

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Help! Help! Help!

    Can someone show me how to shorten my codes? Thanks!

    package javaPackage;
    import javax.swing.*;
    import java.util.*;
    public class Random
    {
    public static void main (String[] args)
    {
    Random input = new Random();
    String games = "", roundTwo = "";
    int user = 0, comp = 0, result = 0;

    games = JOptionPane.showInputDialog(null, "Enter a number: \n1. Rock" +
    "\n2. Paper \n3. Scissors: ", "Decision", JOptionPane.INFORMATION_MESSAGE);
    user = Integer.parseInt(games);

    comp = (int) (Math.random() *3);

    JOptionPane.showMessageDialog(null, "The computer's choice is: " +
    (comp+1), "Computer", JOptionPane.INFORMATION_MESSAGE);

    if (user == 1 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user ==1 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user ==1 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user == 2 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 2 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 2 && comp ==2)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 3 && comp ==0)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 3 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user==3 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else
    JOptionPane.showMessageDialog(null, "Invalid input.", "Error",
    JOptionPane.ERROR_MESSAGE);

    roundTwo = JOptionPane.showInputDialog(null, "Another round? 0 if no, 1 if yes ", "Round",
    JOptionPane.INFORMATION_MESSAGE);
    result = Integer.parseInt(roundTwo);

    while (result == 1)
    {
    games = JOptionPane.showInputDialog(null, "Enter a number: \n1. Rock" +
    "\n2. Paper \n3. Scissors: ", "Decision", JOptionPane.INFORMATION_MESSAGE);
    user = Integer.parseInt(games);

    comp = (int) (Math.random() *3);

    JOptionPane.showMessageDialog(null, "The computer's choice is: " +
    (comp+1), "Computer", JOptionPane.INFORMATION_MESSAGE);

    if (user == 1 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user ==1 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user ==1 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user == 2 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 2 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 2 && comp ==2)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 3 && comp ==0)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if (user == 3 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else if(user==3 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }

    else
    JOptionPane.showMessageDialog(null, "Invalid input.", "Error",
    JOptionPane.ERROR_MESSAGE);

    roundTwo = JOptionPane.showInputDialog(null, "Another round? 0 if no, 1 if yes ", "Round",
    JOptionPane.INFORMATION_MESSAGE);
    result = Integer.parseInt(roundTwo);

    }
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help! Help! Help!

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Nov 2012
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Help! Help! Help!

    Okay. One more, how can I make it array oriented? Please, help!

    package javaPackage;
    import javax.swing.*;
    import java.util.*;
    public class Random 
    {
    public static void main (String[] args)
    {
    Random input = new Random();
    String games = "", roundTwo = "";
    int user = 0, comp = 0, result = 0;
     
    games = JOptionPane.showInputDialog(null, "Enter a number: \n1. Rock" +
    "\n2. Paper \n3. Scissors: ", "Decision", JOptionPane.INFORMATION_MESSAGE);
    user = Integer.parseInt(games);	
     
    comp = (int) (Math.random() *3);	
     
    JOptionPane.showMessageDialog(null, "The computer's choice is: " +
    (comp+1), "Computer", JOptionPane.INFORMATION_MESSAGE);
     
    if (user == 1 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user ==1 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user ==1 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user == 2 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 2 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 2 && comp ==2)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 3 && comp ==0)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 3 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user==3 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else
    JOptionPane.showMessageDialog(null, "Invalid input.", "Error",
    JOptionPane.ERROR_MESSAGE);
     
    roundTwo = JOptionPane.showInputDialog(null, "Another round? 0 if no, 1 if yes ", "Round",
    JOptionPane.INFORMATION_MESSAGE);
    result = Integer.parseInt(roundTwo);
     
    while (result == 1)
    {
    games = JOptionPane.showInputDialog(null, "Enter a number: \n1. Rock" +
    "\n2. Paper \n3. Scissors: ", "Decision", JOptionPane.INFORMATION_MESSAGE);
    user = Integer.parseInt(games);	
     
    comp = (int) (Math.random() *3);	
     
    JOptionPane.showMessageDialog(null, "The computer's choice is: " +
    (comp+1), "Computer", JOptionPane.INFORMATION_MESSAGE);
     
    if (user == 1 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user ==1 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user ==1 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user == 2 && comp == 0)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 2 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 2 && comp ==2)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 3 && comp ==0)
    {
    JOptionPane.showMessageDialog(null, "The computer won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if (user == 3 && comp == 1)
    {
    JOptionPane.showMessageDialog(null, "You won.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else if(user==3 && comp == 2)
    {
    JOptionPane.showMessageDialog(null, "The match is draw.", "Result",
    JOptionPane.INFORMATION_MESSAGE);
    }
     
    else
    JOptionPane.showMessageDialog(null, "Invalid input.", "Error",
    JOptionPane.ERROR_MESSAGE);
     
    roundTwo = JOptionPane.showInputDialog(null, "Another round? 0 if no, 1 if yes ", "Round",
    JOptionPane.INFORMATION_MESSAGE);
    result = Integer.parseInt(roundTwo);
     
    }
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Help! Help! Help!

    Please format the code so that the nested statements are properly indented 3-4 spaces. All the statements should NOT start in the first column.
    Unformatted code is hard to read and understand.

    Instead of repeating the test for the user, have one test for the each user and nest within that if the other ifs:
    if (user1) {
       if(comp1) {
       }else if(comp2) {
       }else{}
    }else if (user2) {
    }
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Help! Help! Help!

    Also, please use more descriptive titles. Something like "Help! Help! Help!" doesn't tell us anything about what you're actually asking, so you're less likely to receive help right away. Norm was nice enough to click on your link, but a more descriptive title makes it clear what you're asking, which makes it more likely more people will try to help. Please see the link in my signature on asking questions the smart way for more information.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Member
    Join Date
    Dec 2012
    Posts
    33
    My Mood
    Confused
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: Help! Help! Help!

    you could use switch statement

    switch(user){
    case 1:
    switch(comp)
    case 1:
    do whatever
    break;
    case 2:
    ...
    }

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    71
    Thanks
    1
    Thanked 7 Times in 7 Posts

    Default Re: Help! Help! Help!

    to clean the code up you can make your roundTwo variable a showConfirmDialog and change your while statement afterwards to an if statement that runs the game again should the user select yes... instead of repeating the exact code again for roundTwo you should make a method that handles the game results (all those if codes you wrote) so that way all you have to do is refer to the method and it will execute the code again for u if the user wants to play again

  8. #8
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: Help! Help! Help!

    if equal
    draw
    if any of the lose condition
    lose
    else
    win