#Description
给两个字符串
第二个字符串是用来猜第一个字符串
如果猜对了
那么该字符串的全部该字母就显示出来
否则就计数器++
如果计数器达到了7,就GG了
问是否GG
同时还有放弃的情况
#Hint
直接cin.nextInt之后nextLine不行
不知为何。。。
#Code
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
for (;;) {
String r = cin.nextLine();
if (r.equals("-1")) break;
System.out.println("Round " + r);
char[] ans = cin.nextLine().toCharArray();
boolean[] b = new boolean[26];
int size = 0;
for (char x : ans) {
if (!b[x - 'a']) size++;
b[x - 'a'] = true;
}
boolean[] c = b.clone();
char[] s = cin.nextLine().toCharArray();
int total = 0;
for (char x : s) {
if (size == 0) break;
if (!b[x - 'a'] && !c[x - 'a']) {
total++;
continue;
}
if (b[x - 'a'] && !c[x - 'a']) {
total++;
continue;
}
c[x - 'a'] = false;
size--;
}
if (total >= 7) {
System.out.println("You lose.");
}else {
if (size == 0)
System.out.println("You win.");
else
System.out.println("You chickened out.");
}
}
}
}
本文深入探讨了一个猜字游戏的实现逻辑,通过分析字符匹配与计数器机制,解释了如何判断玩家是否达到失败条件(计数器达到7),同时涵盖了游戏的放弃情况处理。文章提供了详细的代码示例,展示了如何使用Java实现这一逻辑。
1659

被折叠的 条评论
为什么被折叠?



