-->
当前位置:首页 > 题库 > 正文内容

程序填空题:Logical Operator

Luz4年前 (2021-05-10)题库780
This program reads a line of logical expression with one logical operator and two boolean values, and evaluates the result. A logical expression is like:
```
true and false
```

The result of the expression above is: `false`.

Now, fill in the blanks below.

```Java

import java.util.Scanner;

enum LogicalOp {
@@[and()](1) { // for and
boolean test(boolean p1, boolean p2) {
return @@[p1 && p2](1);
}
},
@@[or ()](1) { // for or
boolean test(boolean p1, boolean p2) {
return @@[p1 || p2](1);
}
};

boolean test(boolean p1, boolean p2) {
return false;
};
}

public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
boolean p1 = in.nextBoolean();
String op = in.next();
boolean p2 = in.nextBoolean();
System.out.println(LogicalOp.@@[valueOf(op)](1).test(p1, p2));
in.close();
}
}
```






答案:
第1空:and()

第2空:p1 && p2

第3空:or ()

第4空:p1 || p2

第5空:valueOf(op)

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。