用java编一个程序,要求如下,希望哪位大虾帮帮忙,很急!!!

2024-12-28 21:43:11
推荐回答(4个)
回答1:

package 娱乐;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.SwingConstants;

public class Text extends JFrame implements ActionListener {
JLabel jl;

JPanel jp1, jp2, jp3;

int a, b;

int zhengque=0 ,cuowu=0;

int A, B, C, D, suiji;

JButton xia, jieshu;

JRadioButton jr1, jr2, jr3, jr4;

ButtonGroup zu;

public Text() {
Container c = getContentPane();
a = mm();
b = mm();
jl = new JLabel(a + "+" + b + "=?");
jp1 = new JPanel();
jp1.add(jl, SwingConstants.CENTER);
c.add(jp1, BorderLayout.NORTH);
xiayiti();
jp2 = new JPanel();
jr1 = new JRadioButton(A + "", false);
jp2.add(jr1);
jr2 = new JRadioButton(B + "", false);
jp2.add(jr2);
jr3 = new JRadioButton(C + "", false);
jp2.add(jr3);
jr4 = new JRadioButton(D + "", false);
jp2.add(jr4);
c.add(jp2, BorderLayout.CENTER);
zu = new ButtonGroup();
zu.add(jr1);
zu.add(jr2);
zu.add(jr3);
zu.add(jr4);
jr1.addActionListener(this);
jr2.addActionListener(this);
jr3.addActionListener(this);
jr4.addActionListener(this);

xia = new JButton("下一题");
jieshu = new JButton("结束");
jp3 = new JPanel();
jp3.add(xia);
jp3.add(jieshu);
c.add(jp3, BorderLayout.SOUTH);
xia.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
chuti();
}
});
jieshu.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {
jl.setText("你答对了:"+zhengque+"道题 "+"答错了:"+cuowu+"道题");
jr1.setEnabled(false);
jr2.setEnabled(false);
jr3.setEnabled(false);
jr4.setEnabled(false);
}
});
setSize(300, 150);
setVisible(true);
}

public void actionPerformed(ActionEvent e) {
int cc = a + b;
if (jr1.isSelected()) {
if (cc == A) {
JOptionPane.showMessageDialog(null, "恭喜你答对了");
zhengque++;
chuti();
}

else{
cuowu++;
JOptionPane.showMessageDialog(null, "错误的选择");
}

}
if (jr2.isSelected()) {
if (cc == B) {
JOptionPane.showMessageDialog(null, "恭喜你答对了");
zhengque++;
chuti();

} else{
cuowu++;
JOptionPane.showMessageDialog(null, "错误的选择");
}
}

if (jr3.isSelected()) {
if (cc == C) {
JOptionPane.showMessageDialog(null, "恭喜你答对了");
zhengque++;
chuti();

} else{
cuowu++;
JOptionPane.showMessageDialog(null, "错误的选择");
}
}

if (jr4.isSelected()) {
if (cc == D) {
JOptionPane.showMessageDialog(null, "恭喜你答对了");
zhengque++;
chuti();

} else{
cuowu++;
JOptionPane.showMessageDialog(null, "错误的选择");
}
}
}
public void chuti() {
a = mm();
b = mm();
jl.setText(a + "+" + b + "=?");
xiayiti();
jr1.setText(A + "");
jr2.setText(B + "");
jr3.setText(C + "");
jr4.setText(D + "");
}
public void xiayiti() {
suiji = 1 + (int) (Math.random() * 4);
if (suiji == 1)
A = a + b;
else
A = xx();
if (suiji == 2)
B = a + b;
else
B = xx();
if (suiji == 3)
C = a + b;
else
C = xx();
if (suiji == 4)
D = a + b;
else
D = xx();
}

public int mm() {
int bv = 10 + (int) (Math.random() * 11);
return bv;
}

public int xx() {
int bb = 20 + (int) (Math.random() * 41);
return bb;
}

public static void main(String[] agrs) {
Text s = new Text();
s.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//用了我1个多小时!!!!

回答2:

你有没有搞错,这种东西你拿出20分?什么叫一个程序?

回答3:

我晚上给你代码

回答4:

三楼写的可以,建议就选它为最佳答案