关于游戏 猜数字的 小问题 (*^__^*)

doudouyeah 2009-12-31 10:05:35
代码如下 小弟实在是修改不了错误了 望高人指点~


错误的地方用红色标注了
代码如下
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.Timer;
import java.util.*;
import java.applet.AudioClip;
import java.io.*;
import java.applet.Applet;
import java.awt.Frame;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.*;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class NumGuess extends JFrame implements ActionListener
{
JTextField jtfInput = new JTextField();
JButton jbOK = new JButton("确定");
JButton jbReplay = new JButton("重玩");
JButton nowtime = new JButton("提示");
JButton othergame = new JButton("其他游戏");
JButton[] jbArray = //按键数组
{
new JButton("9"),
new JButton("8"),
new JButton("7"),
new JButton("6"),
new JButton("5"),
new JButton("4"),
new JButton("3"),
new JButton("2"),
new JButton("1"),
new JButton("0")
};
JPanel jpKey = new JPanel(); //此Panel存放1~9按键
JLabel jlTips = new JLabel(); //提示标签
String answer; //这个是答案
int a,b; //A和B的个数
int count; //猜的次数不能大于8
JLabel[] jlOutput = //输出玩家猜的结果情况
{
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel(""),
new JLabel("")
};
JPanel jpOutput = new JPanel(); //存放玩家每次猜的结果,如 5684:0A0B
JLabel lbl=new JLabel();
Date now=new Date();
public NumGuess()
{






this.setLayout(null);
this.setTitle("KWC猜数字");

jtfInput.setBounds(80,40,250,25);
this.add(jtfInput);

jlTips.setBounds(80,15,300,25);
jlTips.setText("在输入框里输入数字(4个)");
this.add(jlTips);


jbOK.setBounds(120,70,70,25);
jbOK.addActionListener(this);
this.add(jbOK);

jbReplay.setBounds(200,70,70,25);
jbReplay.addActionListener(this);
this.add(jbReplay);

nowtime.setBounds(40,70,70,25);
nowtime.addActionListener(this);
this.add(nowtime);

othergame.setBounds(280,70,70,25);
othergame.addActionListener(this);
this.add(othergame);







jpKey.setBounds(110,90,180,140);
jpKey.setLayout(new FlowLayout());
jpKey.setBackground(new Color(129,150,180));
this.add(jpKey);

for(JButton tjb:jbArray)
{
jpKey.add(tjb);
tjb.addActionListener(this);
}

jpOutput.setBounds(160,230,95,200);
jpOutput.setLayout(new FlowLayout());
for(JLabel tjl:jlOutput)
{
jpOutput.add(tjl);
}
this.add(jpOutput);

this.setBounds(180,180,400,450);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
InitNum();


}



void InitNum()
{//初始化所猜数字
answer = "";
int a;
String ts;
Random rand = new Random();
for(int i=0;i<4;i++)
{
a = (Math.abs(rand.nextInt())%10);
ts =(new Integer(a)).toString();
for(i=0;i<answer.length();i++)
{//判断随机数是否和之前的重复
int index=answer.indexOf(ts);
if(index!=-1)
{
a = (Math.abs(rand.nextInt())%10);
ts =(new Integer(a)).toString();
}
}
answer += ts;
}
}

void compare(String input,String answer)
{//函数:比较输入字符串和答案的一致程度
this.a=0;
this.b=0;
char ci;
for(int i=0;i<input.length();i++)
{
ci=input.charAt(i);
if(ci==this.answer.charAt(i))
{
this.a++;
continue;
}
else
{
for(int j=0;j<this.answer.length();j++)
{
if(ci==this.answer.charAt(j))
{
this.b++;
break;
}
}
}
}
if(a==4)
{
jlTips.setText("恭喜你!答对了!按“重玩”键开始");
}
else
{
jlOutput[count].setText("("+(count + 1) + ") " + input + " : " + this.a + "A" + this.b + "B");
count++;
}
}

public void actionPerformed(ActionEvent e)
{//按键事件响应
for(int i=0;i<jbArray.length;i++)
{
if(e.getSource()==jbArray[i])
{
if(jtfInput.getText().length()!= 4)
{
jtfInput.setText(jtfInput.getText() + (9-i));
break;
}
else
{
jlTips.setText("只能输入4个数字!你会不会玩啊?");
break;
}
}
}
if(e.getSource()==jbOK)
{//按下确定键
String sInput;
jlTips.setText("");
if(jtfInput.getText().length()!=4)
{
jlTips.setText("请输入4个数字,看准喽");
}
else
{
if(count>=8)
{
this.jlTips.setText("都猜了8次了,哎.正确答案是:" + this.answer);
}
else
{
sInput=new String(jtfInput.getText());
this.compare(sInput,answer);
}
jtfInput.setText("");
}

}
if(e.getSource()==jbReplay)
{//按下重玩键
jtfInput.setText("");
jlTips.setText("在输入框里输入数字(4个)");
for(JLabel tjl:jlOutput)
{
tjl.setText("");
}
InitNum();
count=0;
}
if(e.getSource()==nowtime)
{String output= "KWC 趣味猜数字:A前数字代表数字正确并且位置正确;B前数字代表数字正确但是位置不正确";
JOptionPane.showMessageDialog(null, output,
"游戏帮助:提示您如何正确进行游戏 ",
JOptionPane.INFORMATION_MESSAGE);

}
if(e.getSource()==othergame)
{ try{
String command = "freecell.exe";
Process child = Runtime.getRuntime().exec(command);
}
catch (IOException ex)
{}
}



}





public static void main(String args[])
{
String output= "KWC 趣味猜数字:A前数字代表数字正确并且位置正确;B前数字代表数字正确但是位置不正确";

new NumGuess();
JOptionPane.showMessageDialog(null, output,
"游戏帮助:提示您如何正确进行游戏 ",
JOptionPane.INFORMATION_MESSAGE);
try {
URL cb;
File f = new File("E:\\2\\BM.AU");
cb = f.toURL();
AudioClip aau;
aau = Applet.newAudioClip(cb);
//aau.play();
aau.loop();
//循环播放 aau.play() 单曲 aau.stop()停止播放

//frame.setBounds(0, 0, 300, 200);
//frame.setVisible(true); ;

} catch (MalformedURLException e) {
e.printStackTrace();


}
}
}


...全文
148 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
cqhweb 2010-01-01
  • 打赏
  • 举报
回复
研究
M0901dsk 2010-01-01
  • 打赏
  • 举报
回复
学习学习!
sweatcoffee 2010-01-01
  • 打赏
  • 举报
回复
路过,学习
doudouyeah 2009-12-31
  • 打赏
  • 举报
回复
人工置顶 求教啊~~~~~~~~

58,454

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧