UVa 489 Hangman Judge [模拟]

本文深入探讨了一个猜字游戏的实现逻辑,通过分析字符匹配与计数器机制,解释了如何判断玩家是否达到失败条件(计数器达到7),同时涵盖了游戏的放弃情况处理。文章提供了详细的代码示例,展示了如何使用Java实现这一逻辑。

#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.");
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值