如何使用println()获取输出

时间:2015-03-31 18:18:38

标签: java

我想写一个程序,它应该给我R3作为输出。我的代码在这里:

public class Main {
    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        boolean P;
        boolean Q;
        System.out.println("please enter 2 number P and Q");
        System.out.print('\n');
        P = console.nextBoolean();
        Q = console.nextBoolean();

        boolean R1;
        boolean R2;
        boolean R3;

        R1 = (P & Q);
        R2 = (Q == R1);
        R3 = (P == R2);
        System.out.print("the output is");
        System.out.println(R3);
    }
}

我搜索了很多,但我不知道为什么这段代码不起作用。此代码运行但是当我输入PQ时,我看不到任何输出。你能帮帮我吗?

2 个答案:

答案 0 :(得分:0)

代码是对的,它也适用于我..你必须为它输入布尔值才能给你答案。 如果您键入" true"或"假"它工作正常。 你不能喂0s或1s

答案 1 :(得分:0)

如果您想使用0和1,请转到:

Scanner scanner = new Scanner(System.in);
boolean p = scanner.nextInt() != 0;
boolean q = scanner.nextInt() != 0;
boolean r1 = p && q;
boolean r2 = q == r1;
boolean r3 = p == r2;
System.out.println(r3 ? 1 : 0);
相关问题