public class test {
private static Scanner in = new Scanner(System.in)
public static void main(String args[]) {
int level, guessCount = 0
String inputStr, guessStr = ""
showMsg("欢迎尝试猜字母游戏!")
showMsg("请输入游戏级别(5、7、9)")
level = getLevel(in.next())
guessStr = getGuessStr(level)
showMsg("游戏开始,请输入你所猜的" + 蠢饥level 卖档碰+
"个字符序列:(exit-退出, guess-作弊)")
while (!"exit".equals(inputStr = in.next())) {
if ("guess".equals(inputStr)) {
showMsg("偷偷告诉你,答案是:" + guessStr)
} else if (guessStr.equals(inputStr.toUpperCase())) {
showMsg("恭喜你猜对了!正确答案是:" + guessStr)
return
} else {
showGuessState(guessStr, inputStr.toUpperCase(), ++guessCount)
}
}
}
private static void showGuessState(String guessStr, String inputStr, int guessCount) {
int letterRight = 0, positionRight = 0
for (int i=0i<guessStr.length()i++) {
char s = guessStr.charAt(i)
if (inputStr.contains(s + "")) {
letterRight++
if (s == inputStr.charAt(i)) {
positionRight++
}
}
}
showMsg("你猜对" + letterRight + "个字符,"
+ "其中" + positionRight + "个字符的位置正确!"
+ "(总次数-" + guessCount + ", exit-退出)")
}
//获取字符串
private static String getGuessStr(int level) {
String guessStr = ""
Random random = new Random()
for (int i=0i<leveli++) {
char str = (char) ('A' + random.nextInt(26))
if (guessStr.contains(str + "")) {
i--
} else {
guessStr += str
}
}
return guessStr
}
//获取游戏等级
private static int getLevel(String 中谈inputStr) {
switch (inputStr) {
case "5":
return 5
case "7":
return 7
case "9":
return 9
default:
showMsg("请重新输入游戏等级:")
return getLevel(in.next())
}
}
//控制台输出带前缀
private static void showMsg(String msg) {
System.out.println("GuessingGame>" + msg)
}
}
不谢~
这个行不 ????package zhouhu12
import java.util.Arrays
import java.util.Scanner
/**
* 1.难度选项选择;
* 2.随机生成相应数量的字符,并装入数组;
* 3。接受控制台输入的字符并加以比较。输出比较的结果。
* toUpperCase 转换为大写字母 toLowerCase 转换为小写。
*/
public class GuessABC {
public static void main(String[] args) {
int num
do{
guessGame(generate())
System.out.println("1. 重新开始游戏,2.退出游戏")
Scanner sc=new Scanner(System.in)
num=sc.nextInt()
}while(num==1)
System.out.print("游戏结束毁做渣,欢迎下次来纤悄玩。")
}
public static char[] generate(){
Scanner sc=new Scanner(System.in)
int g
do{
System.out.println("欢迎进入猜字母游戏,请选择游戏难度等级:胡厅1.初级 2.中级 3.高级")
g=sc.nextInt()
}while(g<1 || g>4)
char arr[]=new char[g]
for(int i=0i<gi++){
arr[i]=(char)(int)(Math.random()*26+97)
}
return arr
}
public static void guessGame(char arr[]){
int count=0
char arr1[]=new char[arr.length]
do{
System.out.println("请输入"+arr.length+"个字母:(输入 0 退出)")
Scanner sca=new Scanner(System.in)
String A=sca.next()
System.out.println(A)
char arr2[]=A.toCharArray()
arr1=Arrays.copyOf(arr2,3)
if(arr1[0]=='0')
break
System.out.print("答案:")
for(int i=0i<arr.lengthi++){
if(arr[i]==arr1[i]){
count++
System.out.print(arr[i])
}
else{
System.out.print("*")
}
}
System.out.println()
if(count==arr.length){
System.out.println("恭喜你猜对了")
break
}
else{
System.out.println("猜对"+count+"个,继续加油。")
}
count=0
}while(arr1[0]!='0')
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)