Results 1 to 13 of 13
  1. #1
    William Hagen is offline Member
    Join Date
    Sep 2015
    Posts
    13
    Rep Power
    0

    Question Having a Problem with my Java Program For Statement Loop?

    Hi, I'm William Hagen, I'm new to this forum site, and have been working with Java since August of 2015, and I wanted to know what issue with my program is, it's making me so frustrated right now to work with. I mean it works, but the "for" statement loop is not looping this program 10 times like I asked it to, looping it 4-5 times instead. Here's my code, I can't see what's wrong, it looks fine, but I really don't know what's the problem. I've already got a two people in my programming class to help, and they say the same thing "Looks fine, I really don't know what the problem is, maybe it's your "for" statement loop?" Here's the code.



    import java.util.Scanner;
    import java.util.Random;

    public class RPS{

    public static void main(String[] args){

    int computer;
    int user;
    int round;
    int counter=0;
    int ties=0;
    int score=0;

    Scanner reader = new Scanner(System.in);


    System.out.println(" Welcome to Rock, Paper, Scissors: Java Edition!!!! ");
    System.out.println("To play, press 1 for rock, 2 for paper, and three for scissors, have fun!!!!");

    score = counter + ties;
    System.out.println("What do you choose fine sir or madam, rock, paper, or scissors!: ");
    for(round=1; round<=10; round++){
    counter++;
    user = reader.nextInt();

    Random generator = new Random();
    computer = generator.nextInt(3) + 1;

    //Loses, counter2 is not counted each time
    if(user == 1 && computer == 2){
    System.out.println("You lose, choose again.");
    round++;
    }


    if(user == 1 && computer == 3){
    System.out.println("You lose, choose again.");
    round++;
    }


    if(user == 2 && computer == 1){
    System.out.println("You lose, choose again.");
    round++;
    }



    if(user == 2 && computer == 3){
    System.out.println("You lose, choose again.");
    round++;
    }



    if(user == 3 && computer == 1){
    System.out.println("You lose, choose again.");
    round++;
    }


    if(user == 3 && computer == 2){
    System.out.println("You lose, choose again.");
    round++;
    }

    //Wins for counter3, adds up to variable counter.
    else{
    if(user == 1 && computer == 1){
    System.out.println("You win, next round");
    counter++;
    round++;
    }

    if(user == 2 && computer == 2){
    System.out.println("You win, next round");
    counter++;
    round++;
    }

    if(user == 3 && computer == 3){
    System.out.println("You win, next round");
    counter++;
    round++;
    }
    }

    //Ties, does not add up to counter or score, it is its own counter, and does not contribute to the score whatsoever.

    if(computer == user || user == computer){
    System.out.println("Tie!!!!");
    ties++;
    round++;
    }




    }

    System.out.println("Your score is " + score + " and this many ties " + ties);

    }
    }




    Looks perfect, but I can't find the issue. So confused, please help me!!!!


    VARIABLE DEFINITIONS:

    round = "Amount of rounds used in for statement"
    counter = "Counts wins and loses"
    score = "Counts your score by adding wins, loses, and ties"
    ties = "Counts ties"
    user = "For user's number input"
    computer = " For computer's randomly generated number"



    Thanks for any help that could be given, I really need it right now, I'm confused, and I love programming!



    Sincerely Yours,


    William Griff Hagen

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Please edit your post and wrap the code in code tags to make it more readable and easier to understand.

    the "for" statement loop is not looping this program 10 times like I asked it to, looping it 4-5 times
    Does the code change the value of the loop control variable (round) inside the loop? That will change how the loop works.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    William Hagen is offline Member
    Join Date
    Sep 2015
    Posts
    13
    Rep Power
    0

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class RPS{
    
    public static void main(String[] args){
    
    int computer;
    int user;
    int round;
    int counter=0;
    int ties=0;
    int score=0;
    
    Scanner reader = new Scanner(System.in);
    
    
    System.out.println(" Welcome to Rock, Paper, Scissors: Java Edition!!!! "); 
    System.out.println("To play, press 1 for rock, 2 for paper, and three for scissors, have fun!!!!");
    
    score = counter + ties;
    System.out.println("What do you choose fine sir or madam, rock, paper, or scissors!: ");
    for(round=1; round<=10; round++){
    counter++;
    user = reader.nextInt();
    
    Random generator = new Random(); 
    computer = generator.nextInt(3) + 1;
    
    //Loses, counter2 is not counted each time
    if(user == 1 && computer == 2){
    System.out.println("You lose, choose again.");
    round++;
    }
    
    
    if(user == 1 && computer == 3){
    System.out.println("You lose, choose again."); 
    round++;
    }
    
    
    if(user == 2 && computer == 1){
    System.out.println("You lose, choose again.");
    round++;
    }
    
    
    
    if(user == 2 && computer == 3){
    System.out.println("You lose, choose again."); 
    round++;
    }
    
    
    
    if(user == 3 && computer == 1){
    System.out.println("You lose, choose again.");
    round++;
    }
    
    
    if(user == 3 && computer == 2){
    System.out.println("You lose, choose again.");
    round++;
    }
    
    //Wins for counter3, adds up to variable counter.
    else{
    if(user == 1 && computer == 1){
    System.out.println("You win, next round");
    counter++;
    round++;
    }
    
    if(user == 2 && computer == 2){
    System.out.println("You win, next round");
    counter++;
    round++;
    }
    
    if(user == 3 && computer == 3){
    System.out.println("You win, next round");
    counter++;
    round++;
    }
    }
    
    //Ties, does not add up to counter or score, it is its own counter, and does not contribute to the score whatsoever.
    
    if(computer == user || user == computer){
    System.out.println("Tie!!!!");
    ties++;
    round++;
    }
    
    
    
    
    }
    
    System.out.println("Your score is " + score + " and this many ties " + ties);
    
    }
    }

  4. #4
    William Hagen is offline Member
    Join Date
    Sep 2015
    Posts
    13
    Rep Power
    0

    Default Re: Having a Problem with my Java Program For Statement Loop?

    I fixed it, I'll send the repaired code. Turns out score=round and score=counter-loss makes the java for statement loop 10 times. Thanks however, if you find any other issues, please feel free to warn me.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Quote Originally Posted by William Hagen View Post
    if you find any other issues, please feel free to warn me.
    Yup. Learn to indent code blocks and otherwise use horizontal white space to improve readability: Code Conventions for the Java Programming Language: Contents

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

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Having a Problem with my Java Program For Statement Loop?

    I don't understand lines 69 thru 86. Aren't you just checking if computer == user? and lines 91 to 95 seem to be testing the same thing.
    And the == test is symmetric (i.e. a == b implies b == a). So you just need one.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  7. #7
    William Hagen is offline Member
    Join Date
    Sep 2015
    Posts
    13
    Rep Power
    0

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Quote Originally Posted by jim829 View Post
    I don't understand lines 69 thru 86. Aren't you just checking if computer == user? and lines 91 to 95 seem to be testing the same thing.
    And the == test is symmetric (i.e. a == b implies b == a). So you just need one.

    Regards,
    Jim
    I fixed that, saw that too, was an awe inspiring moment for me. Can I PM you the updated Java program with the bug fix.

  8. #8
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Quote Originally Posted by William Hagen View Post
    Can I PM you the updated Java program with the bug fix.
    Actually its better for the forum to post the updated code. Then others may learn.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  9. #9
    William Hagen is offline Member
    Join Date
    Sep 2015
    Posts
    13
    Rep Power
    0

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Here's the updated code.

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class RPS{
    
    public static void main(String[] args){
    
    int computer;
    int user;
    int round=0;
    int counter=0;
    int loss=0;
    int ties=0;
    int score=10;
    
    Scanner reader = new Scanner(System.in);
    
    
    System.out.println(" Welcome to Rock, Paper, Scissors: Java Edition!!!! "); 
    System.out.println("To play, press 1 for rock, 2 for paper, and three for scissors, have fun!!!!");
    
    score = counter + ties;
    score = round;
    score = counter - loss;
    
    for(round=1; round<=10; round++){
    counter++;
    
    System.out.println("What do you choose fine sir or madam, rock, paper, or scissors!: ");
    user = reader.nextInt();
    
    Random generator = new Random(); 
    computer = generator.nextInt(3) + 1;
    
    //Loses, subtracted from variable score.
    if(user == 1 && computer == 2 || user == 2 && computer == 3 || user == 3 && computer == 1){
    System.out.println("You lose, choose again.");
    round++;
    loss++;
    }
    
    
    //Wins for counter, adds up to variable score.
    else{
    if(user == 1 && computer == 1 || user == 2 && computer == 2 || user == 3 && computer == 3){
    System.out.println("You win, next round");
    counter++;
    round++;
    }
    
    
    //Ties, does add up to counter or score, but does not contribute to the score whatsoever.
    
    if(computer == user || user == computer){
    System.out.println("Tie!!!!");
    ties++;
    round++;
    }
    }
    
    
    
    }
    
    //Your official score output.
    System.out.println("Your score is " + score);
    
    
    //Your outputs for the amounts of losses and ties.
    System.out.println("You had this many losses" + loss);
    
    System.out.println("And you had this many ties" + ties);
    
    }
    }

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: Having a Problem with my Java Program For Statement Loop?

    This still doesn't make sense to me. Look at my embedded comments.

    Java Code:
    if(user == 1 && computer == 1 || user == 2 && computer == 2 || user == 3 && computer == 3){
    // in other words, if user == computer.  And wouldn't this be a tie?
    System.out.println("You win, next round");
    counter++;
    round++;
    }
     
     
    //Ties, does add up to counter or score, but does not contribute to the score whatsoever.
     
    if(computer == user || user == computer){
    // This makes sense for ties, but you only need "computer == user"
    System.out.println("Tie!!!");  // just move this print statement to the previous loop.
    ties++; // same here
    round++;  
    }
    And you are still double incrementing the round counter (you need to remove the extraneous round++ statements). I recommend you put in print statements to track your program's progress. Print out state information to ensure things are working the way you expect. And include meaningful debugging statements. E.g. System.out.println("At end of tie conditional, round = " + round). This lets you see what is happening and where.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    William Hagen is offline Member
    Join Date
    Sep 2015
    Posts
    13
    Rep Power
    0

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Quote Originally Posted by jim829 View Post
    This still doesn't make sense to me. Look at my embedded comments.

    Java Code:
    if(user == 1 && computer == 1 || user == 2 && computer == 2 || user == 3 && computer == 3){
    // in other words, if user == computer.  And wouldn't this be a tie?
    System.out.println("You win, next round");
    counter++;
    round++;
    }
     
     
    //Ties, does add up to counter or score, but does not contribute to the score whatsoever.
     
    if(computer == user || user == computer){
    // This makes sense for ties, but you only need "computer == user"
    System.out.println("Tie!!!");  // just move this print statement to the previous loop.
    ties++; // same here
    round++;  
    }
    And you are still double incrementing the round counter (you need to remove the extraneous round++ statements). I recommend you put in print statements to track your program's progress. Print out state information to ensure things are working the way you expect. And include meaningful debugging statements. E.g. System.out.println("At end of tie conditional, round = " + round). This lets you see what is happening and where.

    Regards,
    Jim
    Will do. Thanks by the way.

  12. #12
    William Hagen is offline Member
    Join Date
    Sep 2015
    Posts
    13
    Rep Power
    0

    Default Re: Having a Problem with my Java Program For Statement Loop?

    Java Code:
    import java.util.Scanner;
    import java.util.Random;
    
    public class RPS{
    
    public static void main(String[] args){
    
    int computer;
    int user;
    int round=0;
    int counter=0;
    int loss=0;
    int ties=0;
    int score=10;
    
    Scanner reader = new Scanner(System.in);
    
    
    System.out.println(" Welcome to Rock, Paper, Scissors: Java Edition!!!! "); 
    System.out.println("To play, press 1 for rock, 2 for paper, and three for scissors, have fun!!!!");
    
    score = counter + ties;
    score = round;
    score = counter - loss;
    
    for(round=1; round<=10; round++){
    counter++;
    
    System.out.println("What do you choose fine sir or madam, rock, paper, or scissors!: ");
    user = reader.nextInt();
    
    Random generator = new Random(); 
    computer = generator.nextInt(3) + 1;
    
    //Loses, subtracted from variable score.
    if(user == 1 && computer == 2 || user == 2 && computer == 3 || user == 3 && computer == 1){
    System.out.println("You lose, choose again.");
    round++;
    loss++;
    }
    
    
    //Wins for counter, adds up to variable score.
    else{
    if(user == 1 && computer == 3 || user == 2 && computer == 1 || user == 3 && computer == 2){
    System.out.println("You win, next round");
    counter++;
    round++;
    }
    
    
    //Ties, does add up to counter or score, but does not contribute to the score whatsoever.
    
    if(user == computer){
    System.out.println("Tie!!!!");
    ties++;
    round++;
    }
    }
    
    
    
    }
    
    //Your official score output.
    System.out.println("Your score is " + score);
    
    
    //Your outputs for the amounts of losses and ties.
    System.out.println("You had this many losses" + loss);
    
    System.out.println("And you had this many ties" + ties);
    
    }
    }
    Java program repaired, code above.

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,422
    Blog Entries
    7
    Rep Power
    28

    Default Re: Having a Problem with my Java Program For Statement Loop?

    You should think a bit more before you start coding; you are explicitly writing down all possibilities of the game, and you did it wrong over and over again. That should've triggered you that there must be a more clever way. Let's define paper == 0, scissors == 1, rock == 2. If both players picked the same number, it's a tie:

    Java Code:
    if (computer == human)
       // a tie
    And here's the trick: if the computer == human + 1 modulo 3, i.e. computer == (human+1)%3, the computer wins (try to figure it out); i.e. (continued from the code fragment above)

    Java Code:
    else if (computer == (human+1)%3)
       // computer wins
    else
       // human wins
    That's only six lines of code; compare it to your verbose stuff ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

Similar Threads

  1. While loop/if statement problem
    By totalspelnerd in forum New To Java
    Replies: 3
    Last Post: 06-18-2013, 01:21 PM
  2. Replies: 7
    Last Post: 05-03-2011, 06:41 PM
  3. problem with if statement in while loop of result set
    By kishan.java in forum New To Java
    Replies: 6
    Last Post: 04-09-2011, 04:07 PM
  4. While Loop Problem with Java Assignment Program
    By welsh_rocker in forum New To Java
    Replies: 9
    Last Post: 01-12-2011, 01:55 PM
  5. Beginner's Problem on Loop/If statement
    By obdi in forum New To Java
    Replies: 2
    Last Post: 07-07-2008, 01:41 AM

Tags for this Thread

Posting Permissions

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