System.out.Println被跳过

时间:2016-09-22 08:23:36

标签: java

我应该制作一个简单的石头剪刀代码,但我的大部分印刷品都不起作用。我尝试添加另一个,看看它是否只是我的选择和rand的变量赋值,但它也没有输出任何东西。非常感谢任何帮助!

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!");
                }
              }
            }
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:0)

您的System.out.println正在被跳过,因为您已将其中的一些放入'其他'条件。因此,当' IF' condition为true,跳过else块,sysout也是如此。以下代码应该可以使用!

另外我建议使用else-if来代替嵌套if条件,因为它更具可读性且不易出错。

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!");
                        }
                    }
                }
            }
        }
    }
}

}