Java 正在跳过System.out.Println

Java 正在跳过System.out.Println,java,Java,我本应该做一个简单的石头剪子密码,但我的大部分打印都不起作用。我尝试添加另一个变量,看看它是否只是choose和rand的变量赋值,但它也没有输出任何内容。非常感谢您的帮助 import java.util.Scanner; public class project4 { public static void main(String[] args) { Random in = new Random(); Scanner key = new Scanner(System.i

我本应该做一个简单的石头剪子密码,但我的大部分打印都不起作用。我尝试添加另一个变量,看看它是否只是choose和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!");
                }
              }
            }
          }
        }
      }
    }
  }
}

正在跳过System.out.println,因为您已将其中一些设置为“else”条件。因此,当“IF”条件为true时,将跳过else块和sysout块。下面的代码应该可以工作

此外,我还建议使用elseif代替嵌套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!");
                        }
                    }
                }
            }
        }
    }
}

}

第一步应该始终是,不要开始怀疑java代码不会运行。它很可能不会跳过代码,而是怀疑您自己的逻辑。还有什么不打印?对我来说,在它说你选择了哪一个之后,在它说它选择了什么之前,它应该输出变量rand和choose的值。然后是任何一句话,上面写着你赢了,你输了,或者是平局!没有在eclipse中为我输出。这里有太多的
{}
s,您无法将其清晰地格式化;请把它修好(-1)。这也行。如果你打算使用你的代码,你可能会注意到为什么它不总是打印出来。请使用
else if(condition){
而不是
else{if(condition){
!你保存了制表符和括号,至少在你的程序中是有意义的!