这题也很简单,一路的判断就够了。我总觉得可以把代码精简一下,但是还没想到更好的办法。
有一个值得注意的地方,在第一个题目 A+B和C 中我学习了Scanner类,next()可以获取下一个String类型变量,而nextLine()读取当前行剩余的所有内容,然后把焦点移动到下一行的开头。但是我忘了题目中甲和乙出拳时中间隔着一个空格,也就是说乙读取的内容总是开头带有一个空格。这里把空格消掉就可以了。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
String[] a = new String[n];
String[] b = new String[n];
int winA = 0, loseA = 0, winB = 0, loseB = 0, tie = 0;
int countAC = 0, countAB = 0, countAJ = 0, countBC = 0, countBB = 0, countBJ = 0;
String resultA , resultB ;
for (int i = 0; i < n; i++) {
a[i] = s.next();
b[i] = s.nextLine();
if (a[i].toString().replace(" ", "")
.equals(b[i].toString().replace(" ", ""))) {
tie++;
} else if (a[i].toString().replace(" ", "").equals("C")) {
if (b[i].toString().replace(" ", "").equals("J")) {
winA++;
loseB++;
countAC++;
} else {
loseA++;
winB++;
countBB++;
}
} else if (a[i].toString().replace(" ", "").equals("B")) {
if (b[i].toString().replace(" ", "").equals("C")) {
winA++;
loseB++;
countAB++;
} else {
loseA++;
winB++;
countBJ++;
}
} else if (a[i].toString().replace(" ", "").equals("J")) {
if (b[i].toString().replace(" ", "").equals("B")) {
winA++;
loseB++;
countAJ++;
} else {
loseA++;
winB++;
countBC++;
}
}
}
System.out.println(winA + " " + tie + " " + loseA);
System.out.println(winB + " " + tie + " " + loseB);
if (countAB >= countAC) {
if (countAB >= countAJ) {
resultA = "B";
} else if (countAC >= countAJ) {
resultA = "C";
} else {
resultA = "J";
}
} else {
if (countAB >= countAJ) {
resultA = "C";
} else if (countAC >= countAJ) {
resultA = "C";
} else {
resultA = "J";
}
}
if (countBB >= countBC) {
if (countBB >= countBJ) {
resultB = "B";
} else if (countBC >= countBJ) {
resultB = "C";
} else {
resultB = "J";
}
} else {
if (countBB >= countBJ) {
resultB = "C";
} else if (countBC >= countBJ) {
resultB = "C";
} else {
resultB = "J";
}
}
System.out.println(resultA+" "+resultB);
}
}
搜索
复制