乒乓球比赛分为11分制和21分制,(得分达到了11分或者21分为赢一局)下面输入比赛的获胜情况,分别输出11分制和21分制的比赛结果
其中输入数据中W代表甲得分,L代表乙得分,E代表输入结束,可以多行输入,并忽视E以后的内容。
例如:
输入:
WWWWWWWWWWWWWWWWWWWW
WWWWWLEEE
输出:11分制 第1局: 11:0 第2局:11:0 第3局:3:1
21分制 第1局: 21:0 第2局:4:0
输入:
WWWWWWWLWLWWWWWWWWWWL
LWWWWWWLEEE
输出:
11分制 第1局:9:2 第2局:9:2 第3局:6:1
21分制 第1局:18:3 第2局:6:2
思路:
1)检验输入,当遇到E就停止;
2)将E以后所有的及其E都删除;
3)11分制:将字符串划分成11,11,这样,然后开始计数,最后输出;
4)同样21分制----
代码分享:
package StringDemo;
//乒乓球比赛
//乒乓球比赛分为11分制和21分制,(得分达到了11分或者21分为赢一局)下面输入比赛的获胜情况,分别输出11分制和21分制的比赛结果
//其中输入数据中W代表甲得分,L代表乙得分,E代表输入结束,可以多行输入,并忽视E以后的内容。
//例如:
//输入:
// WWWWWWWWWWWWWWWWWWWW
// WWWWWLEEE
//输出:11分制 第1局: 11:0 第2局:11:0 第3局:3:1
// 21分制 第1局: 21:0 第2局:4:0
//输入:
//WWWWWWWLWLWWWWWWWWWWL
//LWWWWWWLEEE
//输出:
//11分制 第1局:9:2 第2局:9:2 第3局:6:1
//21分制 第1局:18:3 第2局:6:2
import java.util.*;
public class StringDemo15 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<String> arrayList = new ArrayList<>();
while (!sc.hasNext("E")) {
String str = sc.nextLine();
arrayList.add(str);
if (str.contains("E")) {
break;
}
}
//检验输入是否正确
System.out.println(arrayList.size());
for (String s : arrayList) {
System.out.println(s);
}
//将E以后所有的及其E都删除
String total = "";
for (String s : arrayList) {
if (s.contains("E")) {
int index = s.indexOf("E");
total = total + s.substring(0, index);
} else {
total = total + s;
}
}
//检验是否E以及E之后的所有的都去掉了
System.out.println(total);
//11分制度
//一共几局
int num = total.length() / 11;//一共几局
if (total.length() % 11 != 0) {
num = num + 1;
}
//最后一局一共多少个
int countLast = total.length() % 11;
ArrayList<String> arrayList1 = new ArrayList<>();//把这些字符,按照11,11,存起来
int start = 0;
for (int i = 11; i < total.length(); i = i + 11) {
//System.out.println("开始:"+start+" ");
arrayList1.add(total.substring(start, i));
start = i;
}
arrayList1.add(total.substring((num - 1) * 11));
//检验按照11划分的是否正确
for (String s : arrayList1) {
System.out.println(s);
}
//进行计数
ArrayList<HashMap<String, Integer>> arrayListHashMap = new ArrayList<>();
for (String s : arrayList1) {
HashMap<String, Integer> hashMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if ((s.charAt(i) == 'W')) {
if (hashMap.containsKey("W")) {
int oldValue = hashMap.get("W");
hashMap.put("W", oldValue + 1);
} else {
hashMap.put("W", 1);
}
}
}
arrayListHashMap.add(hashMap);
}
//检验计数是否正确
for (HashMap<String, Integer> hashMap : arrayListHashMap) {
System.out.println(hashMap.get("W"));
}
//输出验证是否正确
System.out.print("11分制 ");
for (int i = 0; i < arrayListHashMap.size(); i++) {
if ((i + 1) < num) {
System.out.print("第" + (i + 1) + "局:" + arrayListHashMap.get(i).get("W") + ":" + (11 - arrayListHashMap.get(i).get("W")) + " ");
} else {
System.out.print("第" + (i + 1) + "局:" + arrayListHashMap.get(i).get("W") + ":" + (countLast - arrayListHashMap.get(i).get("W")) + " ");
}
}
//21分制度
//一共几局
int num2 = total.length() / 21;//一共几局
if (total.length() % 21 != 0) {
num2 = num2 + 1;
}
//最后一局一共多少个
int countLast2 = total.length() % 21;
ArrayList<String> arrayList11 = new ArrayList<>();//把这些字符,按照11,11,存起来
int start2 = 0;
for (int i = 21; i < total.length(); i = i + 21) {
//System.out.println("开始:"+start+" ");
arrayList11.add(total.substring(start2, i));
start2 = i;
}
arrayList11.add(total.substring((num2 - 1) * 21));
//检验按照11划分的是否正确
for (String s : arrayList11) {
System.out.println(s);
}
//进行计数
ArrayList<HashMap<String, Integer>> arrayListHashMap2 = new ArrayList<>();
for (String s : arrayList11) {
HashMap<String, Integer> hashMap = new HashMap<>();
for (int i = 0; i < s.length(); i++) {
if ((s.charAt(i) == 'W')) {
if (hashMap.containsKey("W")) {
int oldValue = hashMap.get("W");
hashMap.put("W", oldValue + 1);
} else {
hashMap.put("W", 1);
}
}
}
arrayListHashMap2.add(hashMap);
}
//检验计数是否正确
for (HashMap<String, Integer> hashMap : arrayListHashMap2) {
System.out.println(hashMap.get("W"));
}
//输出验证是否正确
System.out.print("21分制 ");
for (int i = 0; i < arrayListHashMap2.size(); i++) {
if ((i + 1) < num2) {
System.out.print("第" + (i + 1) + "局:" + arrayListHashMap2.get(i).get("W") + ":" + (21 - arrayListHashMap2.get(i).get("W")) + " ");
} else {
System.out.print("第" + (i + 1) + "局:" + arrayListHashMap2.get(i).get("W") + ":" + (countLast2 - arrayListHashMap2.get(i).get("W")) + " ");
}
}
}
}
来源:CSDN
作者:进进进出出
链接:https://blog.csdn.net/wenyunick/article/details/104616228