效果图
一个比较简单的代码, 供参考 ,可以根据自己的需求, 在进行修改
import javaxswing;import javaxswingborder;
import javaawt;
import javaawtevent;
public class GuessNumFrame extends JFrame {
private JPanel contentPane;
private int num;
private final JTextField jtfInput;
private final JLabel label;
public static void main(String[] args) {
EventQueueinvokeLater(new Runnable() {
public void run() {
try {
GuessNumFrame frame = new GuessNumFrame();
framesetVisible(true);//窗口可见
} catch (Exception e) {
eprintStackTrace();
}
}
});
}
public GuessNumFrame() {
setTitle("猜数字");
setDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
setSize(360, 160);//窗口大小
setLocationRelativeTo(null);//居中
contentPane = new JPanel();
contentPanesetBorder(new EmptyBorder(5, 5, 5, 5));
contentPanesetLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JPanel panel_1 = new JPanel();
contentPaneadd(panel_1, BorderLayoutCENTER);
panel_1setLayout(new GridLayout(3, 1, 0, 0));
JPanel panel = new JPanel();
panel_1add(panel);
JButton jbNum = new JButton("得到一个随机数");
paneladd(jbNum);
JPanel panel_2 = new JPanel();
panel_1add(panel_2);
label = new JLabel("");
labelsetOpaque(true);
labelsetBackground(ColorCYAN);
panel_2add(label);
JPanel panel_3 = new JPanel();
panel_1add(panel_3);
jtfInput = new JTextField();
panel_3add(jtfInput);
jtfInputsetColumns(10);
JButton jbOk = new JButton("确定");
panel_3add(jbOk);
jbNumaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
num = (int)(Mathrandom()10);//生成随即数
labelsetText("随机数已经生成范围0~9");
Systemoutprintln(num);
}
});
jbOkaddActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int x;
try {
x = IntegerparseInt(jtfInputgetText());//取得输入框的数字 进行比较
}catch (Exception e1) {
JOptionPaneshowMessageDialog(null, "sorry,输入错误!");
return;
}
if(x==num){
JOptionPaneshowMessageDialog(null, "厉害,猜对了~!");
}else if(x<num){
JOptionPaneshowMessageDialog(null, "你猜小了", "提示",JOptionPaneWARNING_MESSAGE);
}else if(x>num) {
JOptionPaneshowMessageDialog(null, "你猜大了", "提示",JOptionPaneWARNING_MESSAGE);
}
}
});
}
}
import javautilRandom;
import javautilScanner;
/
游戏随即给出一个0~99(包括0和99)的数字,然后让你猜是什么数字。你可以随便猜一个数字,游戏会提示太大还是太小,从而缩小结果范围。经过几次猜测与提示后,最终退出答案。在游戏过程中。记录你最终猜对时所需要的次数。游戏结束后公布结果。见下
次数 结果
1 你太有才了!
2~6 这么快就猜出来了,很聪明么!
大于7 猜了半天才猜出来,小同志,尚需努力啊!
/
public class guessGame {
/
@param args
/
public static void main(String[] args) {
int gameValue = (int)(Mathrandom()()(100-1)+1);
Systemoutprintln("Rand:"+gameValue);
Scanner sc = new Scanner(Systemin);
Systemoutprintln("请输入一个数字");
int num = scnextInt();
int guessCorrectNum=1;
while(true){
if(num==gameValue){
if(guessCorrectNum == 1)
Systemoutprintln("你太有才了!");
else if((guessCorrectNum >=2) && (guessCorrectNum<=6))
Systemoutprintln("这么快就猜出来了,很聪明么");
else if(guessCorrectNum >7)
Systemoutprintln("猜了半天才猜出来,小同志,尚需努力啊!");
break;
}
else{
if (guessCorrectNum <=20){
guessCorrectNum = guessCorrectNum + 1;
num = scnextInt();
}
else{
Systemoutprintln("20次都猜不出来,不让你猜了");
break;
}
}
}
}
}
please tell me your q-number,so I can send it by q
代码如下:
import javautilScanner;public class RandomT {
int sum=0;
public static void main(String[] args) {
int number=(int)(Mathrandom()100+1);
int temp=number;
Systemoutprintln("请猜一个100以内的数:");
RandomT rt=new RandomT();
int in = rtSn();
Boolean yn = rtCompare(temp, in);
while(!yn){
Systemoutprintln("请重新输入:");
int in1 = rtSn();
yn = rtCompare(temp, in1);
}
Systemoutprintln("正确数字是:"+number);
}
public Boolean Compare(int temp,int number){
Boolean yesno = null;
if(temp==number){
if(sum==1){
Systemoutprintln("Beautiful");
}else if(2<=sum||sum<=4){
Systemoutprintln("还是不错的!");
}else{
Systemoutprintln("唉,总算对了!");
}
yesno=true;
}else if(number<temp){
Systemoutprintln("猜小了");
yesno=false;
}else if(number>temp){
Systemoutprintln("猜大了");
yesno=false;
}
return yesno;
}
public int Sn(){
Scanner sc = new Scanner(Systemin);
int in=scnextInt();
sum+=1;
return in;
}
}
如果!
以上就是关于java 用图形化界面编写一个猜数游戏全部的内容,包括:java 用图形化界面编写一个猜数游戏、用JAVA写一个程序 求解 题目:猜数字游戏 随机生成4个0到9的整数,组成一个序列、java课程设计——猜数字游戏等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)