打字母游戏java代码 java打字小游戏

Java 编程求帮助为猜字母游戏添加游戏等级 。游戏等级设为三等:5、7和9,import java.util.*;
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=0;iguessStr.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=0;ilevel;i++) {
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);
}
}
不谢~
程序设计:简单字母打字游戏(JAVA编写)/*
游戏开始,从天上掉下10个有标号(字母)的炸弹,
下面有10颗子弹(就是可以按10个字母).
如果所按字母包含在炸弹中,就找到相应的炸弹并
消灭它,消灭后返回继续利用.如果没有就一直飞到天上最后才返回.
*/
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
public class paodan extends Applet implements Runnable,KeyListener
{
private Image bgImage;
private Graphics bg;
static Thread myThread;
static AudioClip ac1,ac2;
static JFrame md;
String Zi26="";
String pd[]={"a","a","a","a","a","a","a","a","a","a"}; //用来存储10个字母做落下的炮弹
String fj[]={"","","","","","","","","",""}; //用来存储10个字母做反击的防空
int x[]=new int[10];
int y[]=new int[10];
//炸弹的坐标
int x1[]=new int[10];
int x2[]=new int[10];
int y1[]=new int[10];
//火箭的坐标
int life=100;//生命
Toolkit KIT=this.getToolkit();
Image paodan=KIT.createImage("paodan.jpg");
Image huojian=KIT.createImage("huojian.jpg");
public paodan() //用来生成一个包含26个英文字母的字符串Zi26
{for (int i=0;i26;i++)
Zi26+=(char)((int)'A'+i);
}
public void cushi() //初始各种数据,如炮弹与火箭的坐标.
{
try
{URL u1=this.getClass().getResource("huishou.wav");

推荐阅读