标签:
1 package day04; 2 3 import java.util.Scanner; 4 5 public class Fundamental { 6 7 8 9 10 public static void main(String[] args) { 11 12 System.out.println("欢迎尝试猜字母游戏"); 13 14 Scanner scan = new Scanner(System.in); 15 //String arr = scan.next(); 16 char[] input =null; 17 //input = arr.toCharArray(); 18 char[] str=Guess(); 19 for(int k=0;k<5;k++) 20 System.out.print(str[k]); 21 int count=0; 22 System.out.println("游戏开始,请输入你猜的五个字母序列:(exit! 退出)"); 23 while(true){ 24 String arr = scan.next().trim().toUpperCase(); 25 if(arr.equals("EXIT!")){ 26 System.out.println("游戏结束!"); 27 break; 28 } 29 input = arr.toCharArray(); 30 int[] sum= check(str,input); 31 int score=500-10*count; 32 if(sum[0]==5){ 33 System.out.println("恭喜你猜对了,你的得分是:"+ score); 34 35 } 36 else{ 37 System.out.println("你猜对"+sum[1]+"个字符"+",其中有"+sum[0]+"个字符的位置正确"); 38 39 } 40 count++; 41 //check(); 42 //Guess(); 43 } 44 45 } 46 47 48 public static char[] Guess(){ 49 50 char[] str = new char[5]; 51 char[] zmb={‘A‘,‘B‘,‘C‘,‘D‘,‘E‘,‘F‘,‘G‘,‘H‘,‘I‘,‘J‘,‘K‘,‘L‘,‘M‘,‘N‘,‘O‘,‘P‘, 52 ‘Q‘,‘R‘,‘S‘,‘T‘,‘U‘,‘V‘,‘W‘,‘X‘,‘Y‘,‘Z‘}; 53 boolean[] flag = new boolean[zmb.length]; 54 55 for(int i=0;i<str.length;i++){ 56 int num; 57 do{ 58 num = (int)(Math.random()*zmb.length); 59 60 61 }while(flag[num]); 62 str[i]=zmb[num]; 63 flag[num]=true; 64 } 65 return str; 66 } 67 68 public static int[] check(char[] str,char[] input){ 69 int[] sum = new int[2]; 70 for(int i=0;i<5;i++){ 71 for(int j=0;j<5;j++){ 72 if(str[i]==input[j]){ 73 sum[1]+=1; 74 if(i==j){ 75 sum[0]+=1; 76 77 } 78 break; 79 } 80 81 82 } 83 84 } 85 return sum; 86 } 87 88 89 }
标签:
原文地址:http://www.cnblogs.com/xiaziteng/p/4703422.html