-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMootVictor.java
More file actions
52 lines (45 loc) · 1.47 KB
/
MootVictor.java
File metadata and controls
52 lines (45 loc) · 1.47 KB
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
import java.util.Scanner;
public class MootVictor {
public static void main(String[] args) {
// read in the family members attending
Scanner console = new Scanner(System.in);
System.out.println("Enter the members of the moot in order using M or C with no spaces:");
String moot = console.next();
// display the victor
if (victor(moot) == 'M')
System.out.println("Montague Victory!");
else
System.out.println("Capulet Victory!");
console.close();
}
public static char victor(String moot) {
int M = 0; // number of alive M's
int C = 0; // number of alive C's
String remain = new String(moot);
int n = moot.length();
do {
remain = "";
for (int i = 0; i < moot.length(); i++) {
if (moot.charAt(i) == 'M')
if (C == 0) {
remain = remain + "M";
M++;
} else
C--;
else { // "C"
if (M == 0) {
remain = remain + "C";
C++;
} else
M--;
} // end else
} // end for
n = moot.length();
moot = new String(remain);
} while (n != moot.length());
if (M > 0)
return 'M';
else
return 'C';
}
}