当前位置:编程学习 > C/C++ >>

请给以下java猜数字代码加一个对话框版本的

类似于这个

import java.util.Random;
import java.io.*;

public class Guess {
public static void main (String args[]) {
Random rnd = new Random();
int randomNum = rnd.nextInt(100); // 0~99
int guessNum = randomNum - 1;
long time = System.currentTimeMillis();

while (true) {
try {
System.out.print("Guess, what is it (0~99)? ");
guessNum = Integer.parseInt(new BufferedReader(new InputStreamReader(System.in)).readLine());
} catch (IOException ioe) {
System.exit(0);
} catch (NumberFormatException nfe) {
System.out.println("Please enter an integer!");
}
if (guessNum == randomNum)
break;
else if (guessNum > randomNum)
System.out.println("Too big!");
else if (guessNum < randomNum)
System.out.println("Too small!");
System.out.print("Try again. ");
}
System.out.println("You win!");
time = System.currentTimeMillis() - time;
System.out.println("You spent " + (time / 1000) + " seconds.");
if (time > 30000)
System.out.println("Did you fall asleep?");
else if (time > 10000 && time <= 30000)
System.out.println("You can do better!");
else
System.out.println("You genius!");
}
}

追问:有错误 无法运行 

好复杂的代码

答案:三四年前,我初java也编过这样的程序,还好放在空间里,现在弄来献丑。不过我的java是忘光了

文件game.java

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.awt.event.*;
public class game extends JFrame{
cout lotto= new cout(this);

String getTime(){
String time;
Calendar now=Calendar.getInstance();
int hour=now.get(Calendar.HOUR_OF_DAY);
int mintue=now.get(Calendar.MINUTE);
int second=now.get(Calendar.SECOND);
int month=now.get(Calendar.MONTH)+1;
int day=now.get(Calendar.DAY_OF_MONTH);
int year=now.get(Calendar.YEAR);
if(hour<11)
time="Good monring! "+year+"-"+month+"-"+day+" "+hour+":"+mintue+":"+second;
else if(hour<18)
time="Good afternoon! "+year+"-"+month+"-"+day+" "+hour+":"+mintue+":"+second;
else
time="Good evening! "+year+"-"+month+"-"+day+" "+hour+":"+mintue+":"+second;
return time;
}//读取游戏开始时间.

JPanel row1=new JPanel();
String get =getTime();
JLabel print1=new JLabel(get+"感谢你选择",JLabel.CENTER);
JLabel print2=new JLabel("本游戏进行休闲,系统己记住你的游戏时间!",JLabel.CENTER);

JPanel row2=new JPanel();
JPanel row21=new JPanel();
JTextArea text=new JTextArea("请输入一个1-1000整数!",3,25);
JButton play=new JButton("开始");
JButton replay=new JButton("确定");
JButton stop=new JButton("放弃");
JLabel result=new JLabel("游戏结果说明区,备用中……");
JPanel row3=new JPanel();
JPanel row31=new JPanel();
JPanel row32=new JPanel();
JButton name=new JButton(" 确定 ");
JButton 易做图=new JButton(" 性别 ");
JTextField name1=new JTextField("请输入你的名字");
JComboBox 易做图1=new JComboBox();
JLabel unuse=new JLabel(" ");//美化界面,不起任何功能
JTextArea winner=new JTextArea("",4,25);

JPanel row4=new JPanel();
JLabel root=new JLabel("版权所有,翻版必纠",JLabel.CENTER);
JLabel maker=new JLabel("制作者:林彦群 QQ:310442700",JLabel.CENTER);


public Insets getInsets(){
Insets squeeze=new Insets(35,10,15,10);
return squeeze;
}//界面外观调整

public game(){
super("made in Yanqun");
setSize(300,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridLayout layout=new GridLayout(6,1,1,1);
setLayout(layout);
play.addActionListener(lotto);
replay.addActionListener(lotto);
stop.addActionListener(lotto);
name.addActionListener(lotto);

GridLayout layout1=new GridLayout(2,1,2,2);
row1.setLayout(layout1);
row1.add(print1);
row1.add(print2);
add(row1);
GridLayout layout21=new GridLayout(3,1,0,0);
row21.setLayout(layout21);
row21.add(play);
row21.add(replay);
row21.add(stop);
stop.setEnabled(false);
replay.setEnabled(false);
BorderLayout layout2=new BorderLayout();
row2.setLayout(layout2);
row2.add(row21,BorderLayout.WEST);
row2.add(text,BorderLayout.CENTER);
add(row2);
add(result);

GridLayout layout31=new GridLayout(2,1,2,2);
row31.setLayout(layout31);
row31.add(name);
row31.add(易做图);
易做图.setEnabled(false);
name.setEnabled(false);
FlowLayout layout321=new FlowLayout();
易做图1.setLayout(layout321);
易做图1.addItem(" 先生 ");
易做图1.addItem(" 女士 ");
GridLayout layout32=new GridLayout(2,1,2,2);
row32.setLayout(layout32);
row32.add(name1);
row32.add(易做图1);

BorderLayout layout3=new BorderLayout();
row3.setLayout(layout3);
row3.add(row31,BorderLayout.WEST);
row3.add(row32,BorderLayout.CENTER);
row3.add(unuse,BorderLayout.EAST);
add(row3);
add(winner);
winner.setEnabled(false);
GridLayout layout4=new GridLayout(2,1,2,2);
row4.setLayout(layout4);
row4.add(root);
row4.add(maker);
add(row4);


setVisible(true);
}
public static void main(String[] argumnets){
game TM=new game();
}
}

文件cout.java

import javax.swing.*;
import java.awt.event.*;
import java.net.*;
public class cout implements ActionListener{
game gui;
public cout(game in){
gui=in;
}
public void actionPerformed(ActionEvent event){
String command=event.getActionCommand();
if(command=="开始"){
startplaying();
}
if(command=="确定"){
replaying();
}
if(command=="放弃"){
stopplaying();
}
if(command==" 确定 "){
getName();
}
}
int pick;
int Addup=0;//存储游戏者猜数的次数.
void startplaying(){
Addup=1;
pick=(int) Math.floor(Math.random()*1000+1);
String warning=Get();
gui.result.setText(""+warning+"……猜了"+Addup+"次");
gui.play.setEnabled(false);
gui.replay.setEnabled(true);
gui.stop.setEnabled(true);
gui.name.setEnabled(false);
}
void replaying(){
Addup++;
if(Addup<11){
String Warn=Get();
gui.result.setText(""+Warn+""+"……猜了"+Addup+"次");
}
else{
gui.result.setText("做人要厚道,再猜下去就没意思了,答案是"+pick);
gui.text.setText("请输入一个1-1000整数!");
gui.play.setEnabled(true);
gui.replay.setEnabled(false);
gui.stop.setEnabled(false);
}
}
void stopplaying(){

gui.text.setText("请输入一个1-1000整数!");
gui.result.setText("come on,让你的脑子动起来吧!");
gui.play.setEnabled(true);
gui.replay.setEnabled(false);
gui.stop.setEnabled(false);
}
void getName(){
String News;
News=gui.name1.getText();
gui.winner.setText(News+"感谢你对本游戏的支持,由于本我是\nJAVA语言的初学者,写出来的游戏不是很理想,\n希望你能反馈游戏中遇到的BUG,谢谢!");
}
String Get(){
String warn="你再乱输不是数字的东西,小心我翻脸";
try{
int num=Integer.parseInt(gui.text.getText());
if(num<0||num>1000) warn="玩游戏就要专心,不要考验我啊!我不是美女帅哥";
else if(num>pick) warn="加把劲吧!比答案多了那么一点点.";
else if(num==pick){
warn="真聪明,你猜对了!^_^";
gui.play.setEnabled(true);
gui.replay.setEnabled(false);
gui.stop.setEnabled(false);
gui.name.setEnabled(true);
}
else if(num<pick) warn="太小了.再猜一下啊,就快猜对了";
}catch(NumberFormatException e){
gui.text.setText("看你也算是一个读书人,\n不会连1-1000都不认识吧~-~\n点放弃再来,再错我就翻脸");

}//异常处理,防止不数字的输入导致程序错误.
return warn;
}//读取用户输入的信息.
}

import java.util.Random;
public class Guess extends javax.swing.JDialog {
/** A return status code - returned if Cancel button has been pressed */ public static final int RET_CANCEL = 0; /** A return status code - returned if OK button has been pressed */ public static final int RET_OK = 1;
/** Creates new form Guess */ public Guess(java.awt.Frame parent, boolean modal) { super(parent, modal); initComponents(); Random rnd = new Random(); randomNum = rnd.nextInt(100); // 0~99 time =

上一个:NetBeans IDE 6.8 JAVA Swing控件易做图联动下拉列表怎么写?
下一个:java 谁能给我画图组成的股市分析代码?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,