java编写一个“猜数”程序:该程序随机在1到1000的范围中选择一个供用户猜测的整数

用JAVA编一个“猜数”应用程序,该程序随机在1到1000的范围中选择一个供用户猜测的整数,我写的哪里错了?~

帮你修改好了。

package caishuzi;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;


public class Cai {
/**
* @param args
*/static int num;
public static void main(String[] args) {
JFrame f=new JFrame();
JLabel c=new JLabel("Guess a number between 1 and 1000");
JPanel p1=new JPanel();
JPanel p2=new JPanel();
JPanel p3=new JPanel();
JPanel p4=new JPanel();
JPanel p5=new JPanel();
final JTextField a=new JTextField(4);
JButton b=new JButton("获取随机数");
JButton b1=new JButton("确定");
final JTextArea area=new JTextArea();
a.setSize(100, 100);
p1.add(c);
p2.add(a);
p3.add(b);
p4.add(b1);
p5.add(area);
f.setTitle("猜数字");
f.setSize(300, 300);
f.add(p1,BorderLayout.BEFORE_FIRST_LINE);
f.add(p2,BorderLayout.WEST);
f.add(p3,BorderLayout.AFTER_LAST_LINE);
f.add(p4,BorderLayout.AFTER_LINE_ENDS);
f.add(p5,BorderLayout.CENTER);
f.setVisible(true);
b.addActionListener(new ActionListener(){
//Congratulations.You guessed the number


public void actionPerformed(ActionEvent e) {
Random a=new Random();
num=1+a.nextInt(1000);
}
});

b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e1) {
int i=Integer.parseInt(a.getText());
if(i>num)
area.setText("Too high.Try again");
if(i<num)
area.setText("Too low.Try again");
if(i==num)
area.setText("Congratulations.You guessed the number");
}
});

// TODO Auto-generated method stub


}
}

先按start按钮开始
然后就可以填如数字猜了。。按submit提交,看是否猜中。


import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutFocusTraversalPolicy;

public class Test extends JFrame {
private static int randomNumber = 1895;

public static void main(String[] args) {
final JFrame frame = new JFrame();

JLabel question = new JLabel(
"I have a number between 1 and 1000 ,can you guess my number");
final JLabel hint = new JLabel("please input you number");
final JTextField tf = new JTextField(8);
JButton submit = new JButton("submit");
JButton start = new JButton("Start");

final JPanel panel = new JPanel();
panel.add(question);
panel.add(hint);
panel.add(tf);
tf.setSize(40, 80);
panel.add(submit);
panel.add(start);

frame.add(panel);

frame.setBounds(200, 200, 300, 200);
frame.setVisible(true);
frame.setLayout(new FlowLayout());

start.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
randomNumber = (int) (Math.random() * 1000 + 1);
}
});
submit.addActionListener(new ActionListener() {

@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int num = Integer.parseInt(tf.getText());
if (randomNumber != 1895) {
if (randomNumber == num) {
hint.setText("Correct!!");
tf.enable(false);
randomNumber = 1895;
} else if (num >= randomNumber) {
panel.setBackground(Color.RED);
hint.setText("Too Hign");
} else if (num < randomNumber) {
panel.setBackground(Color.BLUE);
hint.setText("Too Low");
}
}
}
});

}
}

Java源程序附后。
本程序的特点是:
(1) 文本框只能输入纯数字;
(2) 界面较美观;
(3) 代码可读性较好,有适当的注释;
(4) 窗体一出现就在桌面居中。

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class GuessNumber {
private static final long serialVersionUID = 1L;
JFrame frame;
JTextField txtNum; //文本框
JButton btnStart; //按钮
JLabel lblPrompt;
JLabel lblMessage;
static int source = 0;
static Random rand = new Random();

public GuessNumber(){
frame = new JFrame("Guess Number");
JPanel pnl1, pnl2, pnl3, pnl4;
pnl1 = new JPanel();
pnl1.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl2 = new JPanel();
pnl2.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl3 = new JPanel();
pnl3.setLayout(new FlowLayout(FlowLayout.LEFT));
pnl4 = new JPanel();
pnl4.setLayout(new FlowLayout(FlowLayout.LEFT));
txtNum = new JTextField(10);
btnStart = new JButton("开始");
lblPrompt = new JLabel("<html><body>I have a number between 1 and 1000 can you guess my number?<br/>Please enter your first guess.</body></html>");
lblMessage = new JLabel();
pnl1.add(lblPrompt);
pnl2.add(txtNum);
pnl3.add(lblMessage);
pnl4.add(btnStart);
frame.setLayout(new GridLayout(4, 1));
frame.add(pnl1);
frame.add(pnl2);
frame.add(pnl3);
frame.add(pnl4);

txtNum.addActionListener(this.new TextAction());
txtNum.addKeyListener(this.new KeyAction());
btnStart.addActionListener(this.new ButtonAction());
frame.setSize(400, 200);
frame.setVisible(true);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
}

public static void main(String[] args) {
new GuessNumber();
while((source=rand.nextInt(1000))==0);
}

//按钮单击后的事件处理
class ButtonAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JButton btn = (JButton)e.getSource();
if(btn == btnStart){
while((source=rand.nextInt(1000))==0);
txtNum.setEditable(true);
}
}
}

//文本框按回车后的事件处理
class TextAction implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
JTextField txt = (JTextField)e.getSource();
if(txt != txtNum){
return;
}
int num = Integer.parseInt(txtNum.getText());
if(num == source){
lblMessage.setText("Correct!");
txtNum.setEditable(false);
txtNum.setBackground(frame.getBackground());
}
else if(num > source){
lblMessage.setText("Too High");
txtNum.setBackground(Color.red);
}
else{
lblMessage.setText("Too Low");
txtNum.setBackground(Color.blue);
}
}
}

//限制文本框只能输入数字
class KeyAction implements KeyListener{
@Override
public void keyPressed(KeyEvent e) {
}

@Override
public void keyReleased(KeyEvent e) {
}

@Override
public void keyTyped(KeyEvent e) {
int k = e.getKeyChar();
String text = ((JTextField)e.getSource()).getText();
if(!((k>47 && k <58) || (k==8 || k==KeyEvent.VK_PERIOD))){ //限制只能输入数字
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
if(text.length() > 4){ //限制数值的长度
e.setKeyChar((char)KeyEvent.VK_CLEAR);
}
}
}
}

java编写一个“猜数”程序:该程序随机在1到1000的范围中选择一个供...
答:Java源程序附后。本程序的特点是:(1) 文本框只能输入纯数字;(2) 界面较美观;(3) 代码可读性较好,有适当的注释;(4) 窗体一出现就在桌面居中。import java.awt.*;import java.awt.event.*;import java.util.*;import javax.swing.*;public class GuessNumber { private static final long ...

用JAVA语言编写一个“猜数字游戏”的程序
答:int num = (int)(Math.random()*100)+1;Scanner sc = new Scanner(System.in);int guessNum = -1;while (guessNum != num) { System.out.println("请输入1-100之间整数");guessNum = sc.nextInt();if (guessNum == num) { System.out.println("中啦");} elseif (guessNum < ...

怎么用java写一个游戏排名界面,最好是有代码和解释,谢谢!
答:java实现的简单猜数字游戏代码,通过随机数与逻辑判断来实现游戏功能 代码如下:import java.util.InputMismatchException;import java.util.Scanner;public class Main { public static void main(String[] args) { // 产生一个随机数 int number = (int) (Math.random() * 100) + 1;// 加入count...

Java猜数字游戏,事先随机给出3个100以内的正确答案,然后再输入100以内...
答:Random r = new Random();int num[] = new int[3];for(int i = 0;i<3;i++){int n = r.nextInt(100);num[i] = n;}System.out.println("请输入数字:");Scanner s = new Scanner(System.in);int input = s.nextInt();boolean res = false;System.out.println("我猜的数字...

1) 编写一个Java猜数的应用程序
答:import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class guessNum { // 猜数次数 int time;// 数字 int number;/ 构造函数 / public guessNum(){ buildNum();} / 生成被猜测的数字 / public void buildNum(){ number=(int) Math.round(Math...

这是小弟自己写的java猜数字小游戏,可是最後怎麼样都不会再猜错5次后...
答:这段程序有这么几个问题:语法报错,编译不能通过,不能通过的原因是:for (n=0; n <= 5; n++) 被您写成了for (; n <= 5; n++)随机数的产生:您是想产生一个1~100之间的整数,用Math.random()是可以的,但是 应该,ran = (int) (Math.round(Math.random() * 99)) + 1; ...

用Java编程实现一个猜数字的游戏:系统随机产生一个1~100的数字,然后让...
答:3.用if语句,当玩家输入的数与随机数相等时(i==num),则输出语句(恭喜你猜对了).4.用else写出猜错是的语句.大致思路就是这样.建议你自己去写,只有自己写过才会懂.如有不懂的M我,本人也在学习中,大家可以相互讨论讨论.另外你可以把数字设置成1~10;这样的话比较容易出现猜对的情况,方便你验证程...

怎么使用java编程: 1、 随机产生一个1-100(小于100)内的整数,由用户通...
答:sc = new java.util.Scanner(System.in); return sc.nextInt(); } public Hello(int a){ //产生随机数 int suiJi = new java.util.Random().nextInt(101)+1; switch(max){ case 0: case 1:

求教Java达人:用java编写一个猜数字游戏
答:package com.zuxia.cg.guest;import java.util.Random;import java.util.Scanner;/ 猜数游戏 系统自动生成4个0-9的不重复数 用户猜 数字和系统生成的数是一样且位置相同就在数那个位置输出a,数相同但位置不同,则在数那个位置输出b 其他数字不变 author student / public class test { / 产生不...

java猜数字游戏?
答:import java.util.Scanner;/ Author: Cool_Wu Date: 2020-12-01 23:39 / public class GuessNumberGame { static int count = 0;static int answer = new Random().nextInt(100);public static void main(String[] args) throws Exception { System.out.println("猜数字游戏开始,该数字是一...

IT评价网,数码产品家用电器电子设备等点评来自于网友使用感受交流,不对其内容作任何保证

联系反馈
Copyright© IT评价网