java新手 求助 字符比较
package cn.sxt.q;import java.util.Random;
import java.util.Scanner;
/**
* 字符猜测小游戏
* @author
* time : 2018.05.12
*/
public class ZiFuCaiCeYouXi {
private static char[] cha = new char[4];//用于存储随机字符
private static char[] chr = new char[4];//用于存储输入字符
private static int[] car = new int[4];//用于存储比较结果
static Scanner scanner = new Scanner(System.in);
static String str1;
public static void main(String[] args) {
while(true){
System.out.println("欢迎来到字符猜测小游戏!");
SuiJi();
for( int i = 0 ; i < 4 ; i++) System.out.print(cha[i]);
System.out.println();
System.out.println("请输入4位字母");
String str = scanner.next();
chr = str.toCharArray();
if(str.length()!=4) System.out.println("请按规定输入字符!");else{
if(car[0] != 4) Pub();
if(str1 == "end") return;
}
}
}
private static void Pub() {
//该方法适用于规定比较结果
ZiFuBiJiao();
car[3] = 500 - (car[2]-1)*10;
if(car[0] != 4){
System.out.println("你猜对的字符给个数为:"+car[1]);
System.out.println("你猜对的字符给位置为:"+car[0]);
}else{
System.out.println("恭喜你猜对了!");
System.out.println("你获得的分数为:"+car[3]);
str1 = "end";
}
System.out.println("输入end结束!");
String str1 = scanner.next();
}
private static void SuiJi() {
//该方法适用于产生随机字符型数组
String str = "abcdefghigklmnopqrstuvwxyz";
Random random = new Random();
int index = random.nextInt(str.length());
for(int i = 0 ; i < 4 ; i++){
cha[i] = str.charAt(index);
}
}
private static void ZiFuBiJiao() {
// 该方法用于两个字符串的比较
int[] q = new int[2];
for ( int i = 0 ; i < 4 ; i++){
if(cha[i]==chr[i]) q[0]++;
for( int j = 0 ; j<4 ; j++) if(cha[i]==chr[j]) q[1]++;
}
for(int i = 0 ; i<4 ; i++) car[i] = q[i];
car[2]++;
}
}