有bug版本:
package fourday; import java.util.Arrays; import java.util.Locale; import java.util.Random; import java.util.Scanner; public class guess_number { public static void main(String[] args){ System.out.println("游戏开始吧:"); char[] aa = one(); show(aa); System.out.println("开始猜吧!"); char[] aa_input = input_char(); show(aa_input); int result[] = two(aa,aa_input); int score = 500; while (result[0] != 5 && result[1] != 5){ System.out.println("本轮猜对" + result[0] + "个字母," + "其中位置正确" + result[1] + "个字母。"); aa_input = input_char(); result = two(aa,aa_input); score = score - 10; } System.out.println("恭喜通关!得分为:" + score); } //生成五个字符串 public static char[] one(){ char[] a1 = new char[5]; String s = "QWERTYUIOPASDFGHJKLZXCVBNM"; char c[] = s.toCharArray(); Random random = new Random(); for (int i = 0; i < 5; i ++){ a1[i] = c[random.nextInt(c.length)]; } return a1; } //输入的字母于与生成的字母做对比 public static int[] two(char[] a1,char[] input){ int result[] = new int[]{0,0}; for (int i = 0;i < a1.length; i++){ for (int j = 0; j < input.length ; j++){ if(a1[i] == input[j]){ result[0] ++; break; } } } for(int i = 0; i < a1.length ; i++){ if (a1[i] == input[i]){ result[1]++; } } return result; } //显示出数组的字母 public static void show(char arr[]){ for(char i:arr){ System.out.print(i); } System.out.println(); } //输入字母 public static char[] input_char(){ System.out.println("请输入5个猜测的字母:"); Scanner scan = new Scanner(System.in); String s = scan.next(); while(s.length() > 5 || s.length() < 5){ System.out.println("输入长度不对,请重新输入:"); s = scan.next(); } s = s.substring(0,5); s = s.toUpperCase(); char input[] = s.toCharArray(); return input; } // public static void input_result(int[] a, int score){ // if (a[0] == 5 && a[1] == 5){ // System.out.println("恭喜通关!得分为:" + score); // } // else{ // System.out.println("本轮猜对" + a[0] + "个字母," + "其中位置正确" + a[1] + "个字母。"); // } // } }
修改后版本:
package fourday; import java.util.Scanner; public class guessing { public static void main(String[] args){ System.out.println("游戏开始!"); char[] gen = generate(); System.out.println(gen); System.out.println("输入5个猜测的字母:"); int count = 0; while(true){ Scanner scan = new Scanner(System.in); String inputs = scan.next().toUpperCase(); if (inputs.equals("EXIT")){ System.out.println("来日再战!"); break; } char input[] = new char[5]; input = inputs.toCharArray(); int result[] = compare(gen,input); if (result[0] == gen.length){ int score = 100 * gen.length - 10 * count; System.out.println("恭喜猜对!" + "得分为:" + score); break; }else{ count ++; System.out.println("本次猜对" + result[1] + "个字母," + "其中位置正确有" + result[0] + "个!"); } } } public static char[] generate(){ char[] chs = new char[5]; String s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; char[] a = s.toCharArray(); //System.out.println(a.length); boolean[] flags = new boolean[a.length]; //System.out.println(a); for (int i = 0; i < 5 ;i++){ int index; do { index = (int)(Math.random()*(a.length)); }while (flags[index] == true); chs[i] = a[index]; flags[index] = true; } return chs; } public static int[] compare(char[] chs,char[] input){ int result[] = new int[2]; for(int i = 0 ;i < chs.length; i++){ for(int j = 0;j < input.length; j ++){ if (chs[i] == input[j]){ result[1]++; if (i == j){ result[0]++; } break; } } } return result; } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)