唐诗缺字游戏

来源:互联网 发布:3306端口对外开放 编辑:程序博客网 时间:2024/05/01 12:30

package com.xinzhanedu;

import java.util.Random;
import java.util.Scanner;

public class PlayGame {
 public static void main(String[] args) {
  int optionIndex = 0;
  Scanner scanner = new Scanner(System.in);
  System.out.println("请输入一首五言律诗");
  String str1 = scanner.next();
  String str2 = scanner.next();
  String str3 = scanner.next();
  String str4 = scanner.next();


  Random random = new Random();

  int index1 = random.nextInt(5);
  int index2 = random.nextInt(5);
  int index3 = random.nextInt(5);
  int index4 = random.nextInt(5);
  String rightAnswer = str1.charAt(index1) + " " + str2.charAt(index2)
    + " " + str3.charAt(index3) + " " + str4.charAt(index4) + " ";
  char[] words = new char[4];
  words[0] = str1.charAt(index1);
  words[1] = str2.charAt(index2);
  words[2] = str3.charAt(index3);
  words[3] = str4.charAt(index4);

  // 出题
  str1 = str1.substring(0, index1) + "_" + str1.substring(index1 + 1);
  str2 = str2.substring(0, index2) + "_" + str2.substring(index2 + 1);
  str3 = str3.substring(0, index3) + "_" + str3.substring(index3 + 1);
  str4 = str4.substring(0, index4) + "_" + str4.substring(index4 + 1);

  System.out.println(str1);
  System.out.println(str2);
  System.out.println(str3);
  System.out.println(str4);

  String[] options = new String[4];
  options[0] = rightAnswer;
  optionIndex++;
  int currentIndex=optionIndex;
  boolean falg = true;
  while (falg) {
   int[] arr = { -1, -1, -1, -1 };
   int i1 = random.nextInt(4);
   int i2 = random.nextInt(4);
   int i3 = random.nextInt(4);
   int i4 = random.nextInt(4);
   arr[i1] = 1;
   arr[i2] = 1;
   arr[i3] = 1;
   arr[i4] = 1;
   boolean falg2 = false;
   for (int i = 0; i < 4; i++) {
    if (arr[i] == -1) {
     falg2 = true;
     break;
    }
   }
   if (falg2 == true) {
    continue;
   }

   String answer = "" + words[i1] + words[i2] + words[i3] + words[i4];
   boolean falg3 = false;
  
   for (int i = 0; i < currentIndex; i++) {
    if (options[i].equals(answer)) {
     falg3 = true;
     break;
    }
   }
   if (falg3 == true) {
    continue;
   }
   options[optionIndex++] = answer;
   if (optionIndex == 4) {
    break;
   }
   currentIndex++;
  }
  while (true) {
   int[] arr = { -1, -1, -1, -1 };
   int i1 = random.nextInt(4);
   int i2 = random.nextInt(4);
   int i3 = random.nextInt(4);
   int i4 = random.nextInt(4);
   arr[i1] = 1;
   arr[i2] = 1;
   arr[i3] = 1;
   arr[i4] = 1;
   boolean falg2 = false;
   for (int i = 0; i < 4; i++) {
    if (arr[i] == -1) {
     falg2 = true;
     break;
    }
   }
   if (falg2 == true) {
    continue;
   }
   System.out.print("A:");
   System.out.println(options[i1]);
   System.out.print("B:");
   System.out.println(options[i2]);
   System.out.print("C:");
   System.out.println(options[i3]);
   System.out.print("D:");
   System.out.println(options[i4]);

   System.out.println("请输入答案A-D");
   String aws = scanner.next();
   if (aws.toUpperCase().equals("A")) {
    if (options[i1].equals(rightAnswer)) {
     System.out.println("恭喜!!");
    } else {
     System.out.println("sorry!");
    }
   }
   if (aws.toUpperCase().equals("B")) {
    if (options[i2].equals(rightAnswer)) {
     System.out.println("恭喜!!");
    } else {
     System.out.println("sorry!");
    }
   }
   if (aws.toUpperCase().equals("C")) {
    if (options[i3].equals(rightAnswer)) {
     System.out.println("恭喜!!");
    } else {
     System.out.println("sorry!");
    }
   }
   if (aws.toUpperCase().equals("D")) {
    if (options[i4].equals(rightAnswer)) {
     System.out.println("恭喜!!");
    } else {
     System.out.println("sorry!");
    }
   }
   break;
  }

 }
}