| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 453 人关注过本帖
标题:怎样记录随即产生的数字
只看楼主 加入收藏
鬼子六
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2007-1-25
收藏
 问题点数:0 回复次数:2 
怎样记录随即产生的数字

随机产生的+-×/运算,我要在界面上添加一个back的按钮,返回上一个题目,怎么写啊,各位帮帮忙。
package com.gui1;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class Calculator implements ActionListener
{
private JFrame frame;
private Container contentPane;

private JButton OKBtn;
private JButton nextBtn;
private JButton ExitBtn;
private JButton outBtn;
private JButton backBtn;

private JLabel num1Lbl;//
private JLabel operLbl;
private JLabel num2Lbl;//
private JTextField answerText;//

private JLabel lblA,lblACount;
private JLabel lblB,lblBCount;
private JLabel lblC,lblCCount;
private JLabel informLbl;

private int countA;//
private int countB;//
private int countC;//

private String oper[]={"+","-","*","/"};
private int n1,n2,n3;
private int operNum;

private Timer timer;
private JLabel lblT1,lblT2;

public Calculator()
{
frame=new JFrame("Pupil Exam System");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(400,200,500,300);
contentPane=frame.getContentPane();
initGUI();
}

public void initGUI()
{
contentPane.setLayout(new CardLayout());
contentPane.add(getPanel(0),"");


//--------------------------------------


}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==outBtn)
{

OKBtn.setEnabled(true);
nextBtn.setEnabled(true);
backBtn.setEnabled(true);

lblACount.setText(0+"");
lblBCount.setText(0+"");
lblCCount.setText(0+"");
outBtn.setEnabled(false);
answerText.setEnabled(true);


lblT1.setText("29");
lblT2.setText("60");
timer.start();

question();

}

if(e.getSource()==OKBtn||e.getSource()==answerText)
{
if(answerText.getText().trim().length()==0)
{
answerText.grabFocus();
return;
}
int n;
try
{
n=Integer.parseInt(answerText.getText().trim());
}catch(Exception e1)
{
informLbl.setText("Please input right answer");
answerText.setText("");
answerText.grabFocus();
return;
}
if(n==getN3())
{
int value=Integer.parseInt(lblACount.getText().trim());
lblACount.setText(value+1+"");

}
else
{
int value=Integer.parseInt(lblBCount.getText().trim());
lblBCount.setText(value+1+"");
}
question();
answerText.setText("");
answerText.grabFocus();
}
if(e.getSource()==nextBtn)
{
int value=Integer.parseInt(lblCCount.getText().trim());
lblCCount.setText(value+1+"");
question();
answerText.grabFocus();
}

if(e.getSource()==ExitBtn)
{
(new Exit()).go();
}

if(e.getSource()==backBtn)
{

}


if(e.getSource()==timer)
{

int value=Integer.parseInt(lblT2.getText().trim());//秒数
int value1=Integer.parseInt(lblT1.getText().trim());//分钟

value--;

if(value>=10)
lblT2.setText(value+"");
else
lblT2.setText("0"+value);
if(value==0)
{
lblT2.setText(60+"");

value1--;
if(value1>=10)
lblT1.setText(value1+"");
else
lblT1.setText("0"+value1);

/*
if(value1==0)
{
timer.stop();
OKBtn.setEnabled(false);
nextBtn.setEnabled(false);
outBtn.setEnabled(false);
answerText.setEnabled(false);
informLbl.setText("");
}*/
}


}
int a=Integer.parseInt(lblACount.getText().trim());
int b=Integer.parseInt(lblBCount.getText().trim());
int c=Integer.parseInt(lblCCount.getText().trim());
if(a+b+c==25)
{
OKBtn.setEnabled(false);
nextBtn.setEnabled(false);
outBtn.setEnabled(false);
answerText.setEnabled(false);
informLbl.setText("test over");
timer.stop();

}


}

public int getN3()
{

if(operNum==0)
n3=n1+n2;
if(operNum==1)
n3=n1-n2;
if(operNum==2)
n3=n1*n2;
if(operNum==3)
n3=n1;
return n3;

}
public void question()
{
operNum=(int)(Math.random()*4);
operLbl.setText(oper[operNum]);
n1=(int)(Math.random()*100)+1;
n2=(int)(Math.random()*100)+1;
if(operNum>=0&&operNum<=2)
{
num1Lbl.setText(n1+"");
num2Lbl.setText(n2+"");
}
else
{
num1Lbl.setText(n1*n2+"");
num2Lbl.setText(n2+"");

}

}
public void selectPanel(int id)
{
CardLayout c=(CardLayout)contentPane.getLayout();
contentPane.add(getPanel(id),"");
c.next(contentPane);
}

public JPanel getPanel(int id)
{
JPanel pAll=null;
switch(id)
{
case 0:
{
Panel p11=new Panel(new FlowLayout());
num1Lbl=new JLabel();
operLbl=new JLabel();
num2Lbl=new JLabel();
answerText=new JTextField(5);
p11.add(num1Lbl);
p11.add(operLbl);
p11.add(num2Lbl);
p11.add(new JLabel("="));
p11.add(answerText);
answerText.setEnabled(false);
JPanel p12=new JPanel(new GridLayout(3,1));
JPanel p121=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel p122=new JPanel(new FlowLayout(FlowLayout.LEFT));
JPanel p123=new JPanel(new FlowLayout(FlowLayout.LEFT));

lblA=new JLabel("right num: ");
lblB=new JLabel("wrong num: ");
lblC=new JLabel("drop num: ");
lblACount=new JLabel();
lblBCount=new JLabel();
lblCCount=new JLabel();

informLbl=new JLabel("Welcome ,please press start button");
p121.add(lblA);
p121.add(lblACount);

p122.add(lblB);
p122.add(lblBCount);

p123.add(lblC);
p123.add(lblCCount);

p12.add(p121);
p12.add(p122);
p12.add(p123);


JPanel p2=new JPanel(new FlowLayout(FlowLayout.RIGHT));
outBtn=new JButton("start");
OKBtn=new JButton("OK");
nextBtn=new JButton("drop");
ExitBtn=new JButton("exit");
backBtn=new JButton("back");

OKBtn.setEnabled(false);
nextBtn.setEnabled(false);
backBtn.setEnabled(false);

lblT1=new JLabel("00");
lblT2=new JLabel("00");



p2.add(outBtn);
p2.add(OKBtn);
p2.add(nextBtn);
p2.add(backBtn);
p2.add(ExitBtn);
p2.add(lblT1);
p2.add(new JLabel(" : "));
p2.add(lblT2);



JPanel p1=new JPanel(new GridLayout(3,1));
p1.add(informLbl);
p1.add(p11);
p1.add(p12);

pAll=new JPanel(new BorderLayout());
pAll.add(p1,BorderLayout.CENTER);
pAll.add(p2,BorderLayout.SOUTH);


outBtn.addActionListener(this);
OKBtn.addActionListener(this);
nextBtn.addActionListener(this);
ExitBtn.addActionListener(this);
backBtn.addActionListener(this);
answerText.addActionListener(this);

timer=new Timer(1000,this);

}break;

case 1:
{


}break;
}
return pAll;
}

public void go()
{
frame.setVisible(true);
}

public static void main(String args[])
{
(new Calculator()).go();
}
}

搜索更多相关主题的帖子: 数字 记录 
2007-06-24 15:06
heilong
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:777
专家分:0
注 册:2007-6-7
收藏
得分:0 

先随机产生个数(在0-3)这之间,每个数代表一个运算符,不就可以了吗?


风水鸡蛋壳,财去人安乐!----->
2007-06-24 16:15
冰山1985
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-6-15
收藏
得分:0 
利用数组,就可以了。
2007-06-24 23:58
快速回复:怎样记录随即产生的数字
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019955 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved