import java.util.Random
import java.util.Scanner
public class test {
public static void main(String[] args) {
Random rnd=new Random()
Scanner in = new Scanner(System.in)
char c=0
String yn = ""
do{
c=(char)('早高A'+rnd.nextInt(27)) //产生的宴培是随机字母
System.out.println("输入C:"+c)
for (int i = 0i <4i++) {
System.out.println("请输入一个大写字母:")
String at = in.next()
System.out.println(at)
String a = String.valueOf(c)
if (a.equals(at)) {
System.out.println("恭喜你,猜对了!")
break
}else {
System.out.println("你猜错了!")
}
}
System.out.println("是否继续:y/n")
yn = in.next().trim()
}while("y".equals(yn))
System.out.println("你结束了猜字母的游戏!")
}
}
//直接粘贴,复制,就能运晌睁唯行。
public class GuessingGame {public static char[] generate(int level){
char[] letters = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T',
'U','V','W','X','Y','Z'}
char[] chs = new char[level]
boolean[] flag = new boolean[26]
for(int i=0i<chs.lengthi++){
int index
do{
index = (int)(Math.random()*26)
}while(flag[index])
chs[i] = letters[index]
flag[index] = true
}
return chs
}
public static int[] check(char[] chs,char[] input){
态芹 int[] result= new int[2]
for(int i=0i<chs.lengthi++){
for(int j=0j<input.lengthj++){
if(chs[i]==input[j]){
result[0]++
if(i==j){
result[1]++
}
break
}
}
}
return result
}
public static void main(String[] args) {
int level
int score
int count=0
Scanner scanner = new Scanner(System.in)
System.out.println("请选择游戏等级")
level = scanner.nextInt()
char[] chs = generate(level)
System.out.println(Arrays.toString(chs)+"\n猜吧")
while(true){
String inputStr = scanner.next().trim().toUpperCase()
char[] input = inputStr.toCharArray()
do {
System.out.println("个数不对,请重新输入")
inputStr = scanner.next().trim().toUpperCase()
input = inputStr.toCharArray()
}while(input.length!=level)
int[] result = check(chs,input)
if("EXIT".equals(inputStr)){
System.out.println("谢谢")
break
}
if(result[1]==level){
score = 500-count*10
System.out.println("恭喜全对!得分"+score)
break
}else{
count++
System.out.println("总次数"+count+"猜对字母橡碧个数"+result[0]+"猜帆如毕对位置个数"+result[1])
}
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)