-
Notifications
You must be signed in to change notification settings - Fork 0
/
HelloWorld.java
78 lines (69 loc) · 1.58 KB
/
HelloWorld.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
*
* author : Yu Chenghui
* matric no: A0194474U
*
*/
import java.util.*;
public class HelloWorld {
public static void main(String[] args) {
// declare the necessary variables
int method;
String type;
int i;
int j;
// declare a Scanner object to read input
Scanner scanner = new Scanner(System.in);
// read input and process them accordingly
method = scanner.nextInt();
// method 1
if (method == 1) {
int num = scanner.nextInt();
for (int count = 0; count < num; count++) {
type = scanner.next();
i = scanner.nextInt();
j = scanner.nextInt();
if (type.equals("AND")) {
System.out.println(i * j);
} else {
System.out.println((i + j == 0) ? 0 : 1);
}
}
} else if (method == 2) {
// method 2
boolean flag2 = false;
boolean flag = scanner.hasNextInt();
if (flag) {
int number = scanner.nextInt();
flag2 = (number == -1 || number == 0);
}
while (!flag2) {
type = scanner.next();
i = scanner.nextInt();
j = scanner.nextInt();
if (type.equals("AND")) {
System.out.println(i * j);
} else {
System.out.println((i + j == 0) ? 0 : 1);
}
flag = scanner.hasNextInt();
if (flag) {
int number2 = scanner.nextInt();
flag2 = (number2 == -1 || number2 == 0);
}
}
} else {
// method 3
while (scanner.hasNext()) {
type = scanner.next();
i = scanner.nextInt();
j = scanner.nextInt();
if (type.equals("AND")) {
System.out.println(i * j);
} else {
System.out.println((i + j == 0) ? 0 : 1);
}
}
}
}
}