今天累惨了,先把代码放上 后天再更
import javax.swing.*;
import java.text.DecimalFormat;
import java.awt.event.*;
public class BinaryEquation{
JFrame frame;
JLabel label1, label2, label3, label4,rightanswer1,rightanswer2,label5,label6;
JTextField slope5 = new JTextField(10);
JTextField slope6 = new JTextField(10);
JButton compare;
long userx, usery;
String y;
String x;
public static void main (String[] args)
{
BinaryEquation gui = new BinaryEquation();
//set parameters
int a = 1, b = 2, c = 3, d = 4;
a = (int) (Math.random()*101);
b = (int) (Math.random()*101);
c = (int) (Math.random()*101);
d = (int) (Math.random()*101);
gui.go(a,b,c,d);
}
public void go(int a,int b,int c,int d)
{
QuadricEquation equa1 = new QuadricEquation();
QuadricEquation equa2 = new QuadricEquation();
equa1.SetParameters(a, b);
equa2.SetParameters(c, d);
//solve the equations to get the right answers
DecimalFormat df = new DecimalFormat();
x=df.format((float)(a-c)/(d-b));
y=df.format((float)(a*d-b*c)/(c-a));
//create the user interface
JFrame frame = new JFrame();
JLabel label1 = new JLabel("Equation1: y = "+ a + "x + "+b);
JLabel label2 = new JLabel("Equation2: y = "+ c + "x + "+d);
JLabel label3 = new JLabel("User's Guess: x =");
JLabel label4 = new JLabel("User's Guess: y =");
label5 = new JLabel();
label6 = new JLabel();
//get user's input
JButton result = new JButton("Result");
frame.setLayout(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(label1);
frame.getContentPane().add(label2);
frame.getContentPane().add(label3);
frame.getContentPane().add(label4);
frame.getContentPane().add(label5);
frame.getContentPane().add(label6);
frame.getContentPane().add(slope5);
frame.getContentPane().add(slope6);
frame.getContentPane().add(result);
rightanswer1 = new JLabel();
rightanswer2 = new JLabel();
frame.getContentPane().add(rightanswer1);
frame.getContentPane().add(rightanswer2);
rightanswer1.setBounds(30, 180, 200, 30);
rightanswer2.setBounds(30, 210, 200, 30);
frame.setSize(300,600);
label1.setBounds(30, 30, 200, 30);
label2.setBounds(30, 60, 200, 30);
label3.setBounds(30, 90, 200, 30);
label4.setBounds(30, 120, 200, 30);
label5.setBounds(30, 240, 200, 30);
label6.setBounds(30, 270, 200, 30);
slope5.setBounds(140, 90, 50, 30);
slope6.setBounds(140, 120, 50, 30);
result.setBounds(60, 150, 100, 30);
frame.setVisible(true);
result.addActionListener(new ButtonListener());
slope5.addActionListener(new textListener1());
slope6.addActionListener(new textListener2());
}
class textListener1 implements ActionListener
{
public void actionPerformed(ActionEvent e1)
{
if(e1.getSource() == slope5)
userx = Long.parseLong(slope5.getText());
}
}
class textListener2 implements ActionListener
{
public void actionPerformed(ActionEvent e1)
{
if(e1.getSource() == slope6)
usery = Long.parseLong(slope6.getText());
}
}
class ButtonListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
rightanswer1.setText("Right Answer: x = " + x);
rightanswer2.setText("Right Answer: y = " + y);
label5.setText("User's Guess: x = "+userx);
label6.setText("User's Guess: y= "+usery);
}
}
}
//y=k*x+b
public class QuadricEquation {
private float Slope,Intercept;
public void SetParameters(float k, float b) //Set parameters slope and intercept
{
Slope = k;
Intercept = b;
}
public float GetSlope() //Get parameters slope and intercept
{
return Slope;
}
public float GetIntercept()
{
return Intercept;
}
}