import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.Calendar;
import java.util.Random;public class GetNumber extends JFrame implements ActionListener
{
//设置一个猜幸运数的界面
JButton button1,button2;
JTextField text2;
JButton button3,button4;
public GetNumber()
{
super("try your luck");
setSize(250,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
FlowLayout layout = new FlowLayout();
setLayout(layout);
button1 = new JButton("获取幸运数字");
button2 = new JButton("显示幸运数字");
button1.addActionListener(this);
button2.addActionListener(this);
text2 = new JTextField("请输入一个0~10的数字",20);
button3 = new JButton("兑奖");
button4 = new JButton("系统时间");
button3.addActionListener(this);
button4.addActionListener(this);
add(button1);
add(button2);
add(text2);
add(button3);
add(button4);
setVisible(true);
}
//对于button事件做出反应 a为随机获取的幸运数,b为用户输入的数字
public void actionPerformed(ActionEvent event)
{
Object string = event.getSource();
int a = 0;
if(string == button1)
{
ForNumber number = new ForNumber(47);
a = number.getlucknumber();
}
if(string == button2)
{
JOptionPane pane = new JOptionPane();
pane.showMessageDialog(null,"你的幸运数是:"+a);
}
if(string == button3)
{
JOptionPane pane = new JOptionPane();
int b = Integer.parseInt(text2.getText());
if(a > b)
{
pane.showMessageDialog(null,"抱歉!你的数太小啦");
}
if(a < b)
{
pane.showMessageDialog(null,"抱歉!你的数太大啦");
}
if(a == b)
{
pane.showMessageDialog(null,"恭喜!你中奖啦!");
}
}
if(string == button4)
{
JOptionPane pane = new JOptionPane();
String string1 = getTime();
pane.showMessageDialog(null,"now time"+string1);
}
}
//获取系统时间
public String getTime()
{
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH)+1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
int second = now.get(Calendar.SECOND);
return ""+year+" "+month+" "+day+" "+hour+":"+minute+":"+second;
}
public static void main(String[] args)
{
GetNumber lucknumber = new GetNumber();
}
}
//生成随机数
class ForNumber extends Random
{
public ForNumber(long a)
{
super(a);
}
public int getlucknumber()
{
return nextInt(10);
}
}为什么获取的随机数他就是不变了,每次都是0;求高手改进一下代码!
解决方案 »
- java中XPath表示中有冒号,如何处理?
- 请问一下,大家在处理反斜杠\的时候怎么做
- 高分, 高难度问题, 请帮助(和classloader) 有关。
- 困扰1个小时的一行语句
- 关于TOMCAT的问题
- 100分求高手来帮助,帮我解决个问题,JFrame怎么载入网页的内容
- des 在java 和.net中的使用
- 高手请进!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
- JDK 1.4.2 with netbean 的问题
- 访问权限
- Java swing中,JMenuItem显示的文字如何分成两部分,分别向两边对齐?
- 算法:寻找没有放入到数组中的数
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.Calendar;
import java.util.Random;public class GetNumber extends JFrame implements ActionListener
{
//设置一个猜幸运数的界面
JButton button1,button2;
JTextField text2;
JButton button3,button4;
int a = 0;
public GetNumber()
{
super("try your luck");
setSize(250,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
FlowLayout layout = new FlowLayout();
setLayout(layout);
button1 = new JButton("获取幸运数字");
button2 = new JButton("显示幸运数字");
button1.addActionListener(this);
button2.addActionListener(this);
text2 = new JTextField("请输入一个0~10的数字",20);
button3 = new JButton("兑奖");
button4 = new JButton("系统时间");
button3.addActionListener(this);
button4.addActionListener(this);
add(button1);
add(button2);
add(text2);
add(button3);
add(button4);
setVisible(true);
} //对于button事件做出反应 a为随机获取的幸运数,b为用户输入的数字
public void actionPerformed(ActionEvent event)
{
Object string = event.getSource();
if(string == button1)
{
ForNumber number = new ForNumber();
a = number.getlucknumber();
//System.out.println(a);
}
if(string == button2)
{
JOptionPane pane = new JOptionPane();
pane.showMessageDialog(null,"你的幸运数是:"+a);
}
if(string == button3)
{
JOptionPane pane = new JOptionPane();
int b = Integer.parseInt(text2.getText());
if(a > b)
{
pane.showMessageDialog(null,"抱歉!你的数太小啦");
}
if(a < b)
{
pane.showMessageDialog(null,"抱歉!你的数太大啦");
}
if(a == b)
{
pane.showMessageDialog(null,"恭喜!你中奖啦!");
}
}
if(string == button4)
{
JOptionPane pane = new JOptionPane();
String string1 = getTime();
pane.showMessageDialog(null,"now time"+string1);
}
}
//获取系统时间
public String getTime()
{
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH)+1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
int second = now.get(Calendar.SECOND); return ""+year+" "+month+" "+day+" "+hour+":"+minute+":"+second;
} public static void main(String[] args)
{
GetNumber lucknumber = new GetNumber();
}
}
//生成随机数
class ForNumber extends Random
{
/*public ForNumber(long a)
{
super(a);
}*/ public int getlucknumber()
{
return nextInt(10);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;
import java.util.Calendar;
import java.util.Random;public class GetNumber extends JFrame implements ActionListener
{
//设置一个猜幸运数的界面
JButton button1,button2;
JTextField text2;
JButton button3,button4;public GetNumber()
{
super("try your luck");
setSize(250,250);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);FlowLayout layout = new FlowLayout();
setLayout(layout);
button1 = new JButton("获取幸运数字");
button2 = new JButton("显示幸运数字");
button1.addActionListener(this);
button2.addActionListener(this);text2 = new JTextField("请输入一个0~10的数字",20);
button3 = new JButton("兑奖");
button4 = new JButton("系统时间");
button3.addActionListener(this);
button4.addActionListener(this);
add(button1);
add(button2);
add(text2);
add(button3);
add(button4);setVisible(true);
}//对于button事件做出反应 a为随机获取的幸运数,b为用户输入的数字
public void actionPerformed(ActionEvent event)
{
Object string = event.getSource();
int a = 0;
if(string == button1)
{
ForNumber number = new ForNumber(47);
a = number.getlucknumber();
}
if(string == button2)
{
JOptionPane pane = new JOptionPane();
pane.showMessageDialog(null,"你的幸运数是:"+a);
}
if(string == button3)
{
JOptionPane pane = new JOptionPane();int b = Integer.parseInt(text2.getText());
if(a > b)
{
pane.showMessageDialog(null,"抱歉!你的数太小啦");
}
if(a < b)
{
pane.showMessageDialog(null,"抱歉!你的数太大啦");
}
if(a == b)
{
pane.showMessageDialog(null,"恭喜!你中奖啦!");
}
}
if(string == button4)
{JOptionPane pane = new JOptionPane();
String string1 = getTime();
pane.showMessageDialog(null,"now time"+string1);
}
}
//获取系统时间
public String getTime()
{
Calendar now = Calendar.getInstance();
int hour = now.get(Calendar.HOUR_OF_DAY);
int minute = now.get(Calendar.MINUTE);
int month = now.get(Calendar.MONTH)+1;
int day = now.get(Calendar.DAY_OF_MONTH);
int year = now.get(Calendar.YEAR);
int second = now.get(Calendar.SECOND);return ""+year+" "+month+" "+day+" "+hour+":"+minute+":"+second;}public static void main(String[] args)
{
GetNumber lucknumber = new GetNumber();
}}
//生成随机数
class ForNumber extends Random
{
public ForNumber(long a)
{
super(a);
}public int getlucknumber()
{
return nextInt(10); 错在这里 应该用return next(10);
}
}