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

Thread: help java code rock paper

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post help java code rock paper

    hello friends, I write code. there is a wrong but I don't find.. Could you help me?
    import javax.swing.JOptionPane;
     
    public class assignment {
        public static void main(String[] args) {
     
            String person = "";
            int computer;
            int scorePerson = 0, scoreComputer = 0;
            String Rock = "", Paper = "", Scissiors = "";
            computer = (int) (Math.random() * 3);
     
            JOptionPane.showMessageDialog(null, "Game start" + "\nPlease choose any one");
            String computer1 = String.valueOf(computer);
     
     
            while(scoreComputer<5 || scorePerson<5) {
     
     
                person = JOptionPane.showInputDialog("scoreComputer : " + scoreComputer + "\nscorePerson : " + scorePerson + "\n1.Rock "
                        + "\n2.Paper " + "\n3.Scissors");
     
     
     
               switch(computer){
                   case 0 :
                       JOptionPane.showMessageDialog(null, "Rock");
                       computer1 = Rock;
                   break;
                   case 1 :
                       JOptionPane.showMessageDialog(null, "Paper");
                       computer1 = Paper;
                       break ;
                   case 2 :
                       JOptionPane.showMessageDialog(null, "Scissiors");
                       computer1= Scissiors;
                       break;
               }
               for(int i = 0 ; i<5;++i) {
     
                   if (person == computer1) {
                       JOptionPane.showMessageDialog(null, "Scoreless");
                   } else {
                       if ((person.equals(Rock) && computer1.equals(Paper)) || (person.equals(Paper) && computer1.equals(Scissiors)) ||
                               (person.equals(Scissiors) && computer1.equals(Rock))) {
                           JOptionPane.showMessageDialog(null, "Computer is won");
                           scoreComputer++;
                           break;
                       } else
                           JOptionPane.showMessageDialog(null, "Person is won");
                       scorePerson++;
                       break;
                   }
               }
     
     
     
     
     
     
     
     
               while (scoreComputer == 5 || scorePerson == 5) {
                   if (scorePerson < scoreComputer) {
                       JOptionPane.showMessageDialog(null, "End of match. Computer is won");
                   } else if (scoreComputer == scorePerson) {
                       JOptionPane.showMessageDialog(null, "End of match. Scoreless");
                   } else
                       JOptionPane.showMessageDialog(null, "End of match. Person is won");
              break; }
            }
     
     
     
     
     
     
     
     
        }
    }
    Last edited by dayiahmet; November 22nd, 2020 at 10:23 AM.

  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 java code rock paper

    there is a wrong but I don't find
    Please explain. Copy the contents of the window and paste it here that shows what you are asking about.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES 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 2020
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: help java code rock paper

    I always win and when I enter something wrong it continues. I could not solve these problems

  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 java code rock paper

    One problem I see: Use the equals() method to compare the contents of String objects, not the == operator.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java code to execute the java code on schedule basis
    By reddysanam in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 16th, 2017, 04:06 AM
  2. Replies: 0
    Last Post: May 23rd, 2013, 04:35 PM
  3. Trouble Porting my Java File Reading Code to Android Code
    By Gravity Games in forum Android Development
    Replies: 0
    Last Post: December 6th, 2012, 04:38 PM
  4. Replies: 5
    Last Post: November 14th, 2012, 10:47 AM
  5. Replies: 3
    Last Post: September 3rd, 2012, 11:36 AM