this is the code that I've done.
import javax.swing.JOptionPane;
public class random2
{
public static void main(String[] johji)
{
for(int x=0;x<=0;x--)
{
int n = (int)(1+Math.random()*9);
int n2 = (int)(1+Math.random()*9);
JOptionPane.showMessageDialog(null,n +" times "+ n2);
int ans = n * n2;
int input=Integer.parseInt(JOptionPane.showInputDialog("Enter your answer"));
if(input==ans)
{
int score = (int)(1+Math.random()*3);
if(score==1)
JOptionPane.showMessageDialog(null,"your answer is correct");
if(score==2)
JOptionPane.showMessageDialog(null,"very good");
if(score==3)
JOptionPane.showMessageDialog(null,"good job");
}
else
JOptionPane.showMessageDialog(null," wrong ");
}
}
}
now my only problem is to put this to JFrame ;((
this my unfinished code ... I really don't know how to ask the user to input the answer and what are the codes should I use.
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Container;
public class Assign extends JApplet implements ActionListener {
JLabel titleLabel = new JLabel("Multiplication Program");
JLabel subtitle = new JLabel("Multiplication Program");
JTextField question = new JTextField(50);
JTextField answer = new JTextField(10);
JButton ok = new JButton("OK");
JButton clear = new JButton("Clear");
final int size = 3;
int b[]=new int[size];
public void init() {
Container c = getContentPane();
c.setLayout(new FlowLayout());
c.add(titleLabel);
c.add(subtitle);
c.add(question);
answer.addActionListener(this);
c.add(ok);
c.add(clear);
question.setEditable(false);
}
public void ok()
{
for(int x=0;x<=0;x--)
{
int n = (int)(1+Math.random()*9);
int n2 = (int)(1+Math.random()*9);
question.setText(n1+"times"+n2);
int ans = n * n2;
}
}
}

New Topic/Question
Reply



MultiQuote



|