标签:
Package a;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
class WindowButton extends JFrame implements ActionListener{ /** *
*/
private static final long serialVersionUID = 1L;
int number;
JTextField inputNumber;
JLabel feedBack;
JButton buttonGetNumber,buttonEnter;
WindowButton(String s){ super("猜数字小游戏");
buttonGetNumber=new JButton("得到一个随机数");
feedBack=new JLabel("无反馈信息",JLabel.CENTER);
feedBack.setBackground(Color.green);
inputNumber=new JTextField("0",5);
buttonEnter=new JButton("确定");
buttonEnter.addActionListener(this);
buttonGetNumber.addActionListener(this);
buttonGetNumber.addActionListener(this);
Box boxH1=Box.createHorizontalBox();
boxH1.add(new JLabel("获取1-100之间的随机数:"));
boxH1.add(buttonGetNumber);
Box boxH2=Box.createHorizontalBox();
boxH2.add(new JLabel("输入您的猜测: "));
boxH2.add(inputNumber);
Box boxH3=Box.createHorizontalBox();
boxH3.add(new JLabel("单击确定按钮: "));
boxH3.add(buttonEnter);
Box boxH4=Box.createHorizontalBox();
boxH4.add(new JLabel("反馈信息: "));
boxH4.add(feedBack);
Box baseBox=Box.createVerticalBox();
baseBox.add(boxH1);
baseBox.add(boxH2);
baseBox.add(boxH3);
baseBox.add(boxH4);
Container con=getContentPane();
con.setLayout(new FlowLayout());
con.add(baseBox);
con.validate();
setBounds(120,125,279,200);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100,100,150,150);
setVisible(true);
validate(); }
public void actionPerformed(ActionEvent e){
if(e.getSource()==buttonGetNumber){
number=(int)(Math.random()*100)+1; }
else if(e.getSource()==buttonEnter){
int guess=0;
try{
guess=Integer.parseInt(inputNumber.getText());
if(guess==number){
JOptionPane.showMessageDialog(this,"猜对了"); }
else if(guess>number){
JOptionPane.showMessageDialog(this,"猜大了");
inputNumber.setText(null); }
else if(guess<number){
JOptionPane.showMessageDialog(this,"猜小了");
inputNumber.setText(null); } }
catch(NumberFormatException event){
JOptionPane.showMessageDialog(this,"请输入数字字符"); } }
} }
public class 猜数字游戏 {
public static void main(String args[]){
new WindowButton("猜数字小游戏"); }
}
package b;
import java.util.Date;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
//给定生成随机数的范围1~1000
Scanner input = new Scanner(System.in);
int num = (int) (Math.random() * 100) + 1;
System.out.println("系统已经生成数字,猜猜看这个数字是多少?(如果实在猜不出来,就输入‘投降‘吧)");
Date d1 = new Date();
int gNum = 0;
int count = 0;
boolean isHandup = false;
do {
count++;
System.out.print("第" + count + "次:");
try {
gNum = input.nextInt();
if (gNum < num) {
System.out.println("猜小了");
}
if (gNum > num) {
System.out.println("猜大了");
}
} catch (Exception ex) {
if (input.next().trim().equals("投降")) {
isHandup = true;
break;
}
System.out.println("输入数字格式不正确,请重新输入。");
count--;
continue;
}
} while (gNum != num);
Date d2 = new Date();
int time = (int) ((d2.getTime() - d1.getTime()) / 1000 + 0.5);
if (isHandup) {
System.out.println("悲剧,你投降了!共用了" + time + "秒,猜了" + (count-1)
+ "次。");
isHandup=false;
} else {
if (count * time < 10) {
System.out.println("太棒了!你运气真好!共用了" + time + "秒,猜了" + count
+ "次。");
} else if (count * time < 50) {
System.out.println("你真聪明!共用了" + time + "秒,猜了" + count + "次。");
} else if (count * time < 1000) {
System.out.println("真棒!你竟然猜出来了!共用了" + time + "秒,猜了" + count
+ "次。");
} else {
System.out.println("终于猜对了,你也太笨 了吧。。。共用了" + time + "秒,猜了" + count
+ "次。");
}
}
}
}
标签:
原文地址:http://www.cnblogs.com/xx18256514526/p/5560674.html