简体   繁体   中英

System.out.Println being skipped

I am supposed to be making a simple rock paper scissors code but most of my println's aren't working. I tried adding another one to see if it was just my variable assignment for choose and rand but it doesn't output anything either. Any help is much appreciated!

import java.util.Scanner;

public class project4 {

  public static void main(String[] args) {
    Random in = new Random();
    Scanner key = new Scanner(System.in);
    System.out.println("Please select one of [R/P/S]: ");
    String choice = key.nextLine();
    int rand = in.nextInt(3) + 1;
    int choose = 0;
    if (choice.equals("r") || choice.equals("R")) {
      choose = 1;
      System.out.println("You chose Rock");
    } else {
      if (choice.equals("P") || choice.equals("p")) {
        choose = 2;
        System.out.println("You chose Paper");
      } else {
        if (choice.equals("s") || choice.equals("S")) {
          choose = 3;
          System.out.println("You chose Scissors");
        }
      }
      System.out.println("rand= " + rand + "choose =" + choose);
      System.out.flush();
    }
    if (rand == 1) {
      System.out.println("I chose Rock");
    } else {
      if (rand == 2) {
        System.out.println("I chose Paper");
      } else {
        System.out.println("I chose Scissors");
      }

      if (choose == rand) {
        System.out.println("Its a Tie!");
      } else {
        if (choose == 1 && rand == 2) {
          System.out.println("Paper beats Rock, You lose!");
        } else {
          if (choose == 1 && rand == 3) {
            System.out.println("Rock beats Scissors, You win!");
          } else {
            if (choose == 2 && rand == 1) {
              System.out.println("Paper beats Rock, You win!");
            } else {
              if (choose == 2 && rand == 3) {
                System.out.println("Scissors beats Paper, You lose!");
              } else {
                if (choose == 3 && rand == 1) {
                  System.out.println("Rock beats Scissors, You lose!");
                } else {
                  System.out.println("Scissors beats Paper, You win!");
                }
              }
            }
          }
        }
      }
    }
  }
}

Your System.out.println are being skipped because you have put some of them inside the 'else' conditions. Due to this when the 'IF' condition is true the else block is skipped and so is the sysout's. The below code should work!

Also I would suggest using else-if in place of the nested-if conditions as it make is more readable and less error prone.

package project4;
import java.util.Random;
import java.util.Scanner;
public class project4 {

public static void main(String[] args) {
    Random in = new Random();
    Scanner key = new Scanner(System.in);
    System.out.println("Please select one of [R/P/S]: ");
    String choice = key.nextLine();
    int rand = in.nextInt(3) + 1;
    int choose = 0;
    if (choice.equals("r") || choice.equals("R")) {
        choose = 1;
        System.out.println("You chose Rock");
    } else {
        if (choice.equals("P") || choice.equals("p")) {
            choose = 2;
            System.out.println("You chose Paper");
        } else {
            if (choice.equals("s") || choice.equals("S")) {
                choose = 3;
                System.out.println("You chose Scissors");
            }
        }
    }
    System.out.println("rand= " + rand + "choose =" + choose);
    System.out.flush();
    if (rand == 1) {
        System.out.println("I chose Rock");
    } else {
        if (rand == 2) {
            System.out.println("I chose Paper");
        } else {
            System.out.println("I chose Scissors");
        }
    }

    if (choose == rand) {
        System.out.println("Its a Tie!");
    } else {
        if (choose == 1 && rand == 2) {
            System.out.println("Paper beats Rock, You lose!");
        } else {
            if (choose == 1 && rand == 3) {
                System.out.println("Rock beats Scissors, You win!");
            } else {
                if (choose == 2 && rand == 1) {
                    System.out.println("Paper beats Rock, You win!");
                } else {
                    if (choose == 2 && rand == 3) {
                        System.out.println("Scissors beats Paper, You lose!");
                    } else {
                        if (choose == 3 && rand == 1) {
                            System.out.println("Rock beats Scissors, You lose!");
                        } else {
                            System.out.println("Scissors beats Paper, You win!");
                        }
                    }
                }
            }
        }
    }
}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM