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;求高手改进一下代码!

解决方案 »

  1.   

    把a作为全局变量,另外创建ForNumber对象不要加参数,否则无论如何都不会产生随机数的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;
    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);
    }
    }
      

  2.   


    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);
    }
    }