Results 1 to 5 of 5

Thread: Why wont this work?

  1. #1
    Join Date
    Jun 2007
    Location
    Inman, SC
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why wont this work?

    im making a paper rock scissors game in java. I got 2 versions. The first one you type in a number representing rock paper or scissors and it works fine. the second one you have to type in rock paper or scissors. This one is giving me trouble. Im trying to make it where once you type in what you want it converts it to a number. ill post the code below
    Code:
    // Brandon Conway
    // Feb 14, 2008
    // Rock Paper Scissors
    import javax.swing.JOptionPane;
    import java.util.*;
    public class PRS2
    {
    public static void main(String[] args)
    {
    
    // User choosing rock paper or scissors
    String choiceString = ""; 
    String chosenItem;
    String rock = "rock";
    String paper = "paper";
    String scissors= "scissors";
    
    int itemType;
    final String R = "Rock";
    final String P = "Paper";
    final String S = "Scissors";
    
    int w = 0;
    int l = 0;
    int t = 0;
    
    for (int rnd=1; rnd<=10; rnd++) {
    
    choiceString = JOptionPane.showInputDialog(null, 
    "What do you choose?" + 
    "\nType it in");
    
    if (choiceString.equalsIgnoreCase(rock));
    {
    choiceString = 1;
    itemType = Integer.parseInt(choiceString);
    }
    else if (choiceString.equalsIgnoreCase(paper));
    {
    choiceString = 2;
    itemType = Integer.parseInt(choiceString);
    }
    else if (choiceString.equalsIgnoreCase(scissors));
    {
    choiceString = 3;
    itemType = Integer.parseInt(choiceString);
    }
    else 
    JOptionPane.showMessageDialog(null, 
    "Try again");
    
    
    if(itemType == 1)
    chosenItem = R;
    else if (itemType == 2)
    chosenItem = P;
    else
    chosenItem = S;
    
    JOptionPane.showMessageDialog(null, 
    "You chose " + chosenItem + ".");
    // End of user choosing item
    
    // Computer choosing item
    Random r = new Random();
    int randint = r.nextInt(3);
    
    if(randint == 0)
    JOptionPane.showMessageDialog(null,"The computer chooses Rock.");
    else if (randint == 1)
    JOptionPane.showMessageDialog(null,"The computer chooses Paper.");
    else
    JOptionPane.showMessageDialog(null,"The computer chooses Scissors.");
    
    // End computer choosing
    
    // Determine Winner if computer chooses rock
    if(randint == 0 && itemType == 1)
    {
    t = t + 1;
    JOptionPane.showMessageDialog(null,"Its a tie.");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    else if (randint == 0 && itemType == 2)
    {
    w = w + 1;
    JOptionPane.showMessageDialog(null,"You Win! Paper beats Rock!");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    else if(randint == 0 && itemType == 3)
    {
    l = l + 1;
    JOptionPane.showMessageDialog(null,"You Lose. Rock beats Scissors.");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    // Determine Winner if computer chooses paper
    else if(randint == 1 && itemType == 1)
    {
    l = l + 1;
    JOptionPane.showMessageDialog(null,"You Lose. Paper beats Rock.");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    else if(randint == 1 && itemType == 2)
    {
    t = t + 1;
    JOptionPane.showMessageDialog(null,"Its a tie.");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    else if(randint == 1 && itemType == 3)
    {
    w = w + 1;
    JOptionPane.showMessageDialog(null,"You Win! Scissors beats Paper!");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    // Determine Winner if computer chooses scissors
    else if(randint == 2 && itemType == 1)
    {
    w = w + 1;
    JOptionPane.showMessageDialog(null,"You Win! Rock Beats Scissors!");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    else if(randint == 2 && itemType == 2)
    {
    l = l + 1;
    JOptionPane.showMessageDialog(null,"You Lose. Scissors Beats Paper.");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    
    else if(randint == 2 && itemType == 3)
    {
    t = t + 1;
    JOptionPane.showMessageDialog(null,"Its a tie.");
    JOptionPane.showMessageDialog(null,"Won = " + w + "  Lost = " + l + "  Tie = " + t);
    }
    }
    
    System.exit(0);
    }
    }

  2. #2
    Join Date
    Jun 2007
    Location
    Inman, SC
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    500 views and not 1 response?

  3. #3
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    I am guessing they all believed you mixed up the name and really meant Javascript

    I am not 100% familiar with java, but you are trying to type cast a string to an integer. What I would suggest is to create integer constants 1/2/3 corresponding with rock/paper/scissors as you have declared them and just use the constant to assign the itemType.

  4. #4
    Join Date
    Jun 2007
    Location
    Inman, SC
    Posts
    25
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i really mean java, its for java class, what i was trying to do was convert the string rock, paper, or scissors, to its corresponding number. But it doesnt seem to want to, thanks for the help Ill go try it out.

  5. #5
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    yes I know you mean real Java, I was saying why people looked but didn't respond.

    if you would prefer not to use the constant method like I mentioned, use this type casting method

    Code:
    (int) choiceString = __#__;

Bookmarks

Posting Permissions

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