GUI随机方程生成器

时间:2014-05-16 03:33:02

标签: java user-interface numbers generator equation

我是初学者。我正在制作一个GUI随机方程生成器,但是当我尝试编译它时,它说"找不到符号方法Random"

import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

import java.sql.*;

import java.util.Random;

public class Math extends JFrame implements ActionListener
{
int Num1;
int Num2;
int Operation;
String Operator;

JButton OK = new JButton ("OK");
JLabel Question = new JLabel ("How much is " + Num1 + " " + Operator + " " + Num2);
JTextField Answer = new JTextField (20);

Container c = getContentPane();

public Math()
{
    c.setLayout(new FlowLayout());
    c.add (Question);
    c.add (Answer);

    Answer.addActionListener(this);

    setVisible(true);
}

public void actionPerformed (ActionEvent e)
{
    if (e.getSource() == OK)
    {
        Num1 = (int)(0 + Math.random()*9);
        Num2 = (int)(0 + Math.random()*9);
        Operation = (1+ Math.random()*4);           
    }
}

public static void Main (String args[])
{
    new Math();
}
}

为什么我收到此错误?

1 个答案:

答案 0 :(得分:1)

尝试创建一个Random对象.Like:Random random = new Random(); 然后你就可以使用它:

Num1 = 0 + random.nextInt(9);
Num2 = 0 + random.nextInt(9);
Operation = 1+ random.nextInt(4);