基本的代码,完成题干的:
package homework01;
import java.io.IOException;
import java.util.Scanner;
/**
* 支持真分数的四则运算。
* @author 陈晨辰尘
*
*/
public class Operation01 {
public static void main (String arg[]) throws IOException {
String _answer="520";
int answer=520;
int random1=(int)(1+Math.random()*(99-1+1));
int random2=(int)(1+Math.random()*(99-1+1));
int random=(int)(1+Math.random()*(8-1+1));
int random3=(int)(1+Math.random()*(10-1+1));
int random4=(int)(1+Math.random()*(10-1+1));
int random5=(int)(1+Math.random()*(99-1+1));
int random6=(int)(1+Math.random()*(99-1+1));
int molecular=(int)(1+Math.random()*(9-1+1));
int denominator=(int)(1+Math.random()*(9-1+1));
while(molecular>=denominator) {
molecular=(int)(1+Math.random()*(9-1+1));
denominator=(int)(1+Math.random()*(9-1+1));
}
Fraction Frac1=new Fraction(molecular,denominator);
molecular=(int)(1+Math.random()*(9-1+1));
denominator=(int)(1+Math.random()*(9-1+1));
while(molecular>=denominator) {
molecular=(int)(1+Math.random()*(9-1+1));
denominator=(int)(1+Math.random()*(9-1+1));
}
Fraction Frac2=new Fraction(molecular,denominator);
while(random5%random6!=0) {//除法都是整除的
random5=(int)(1+Math.random()*(99-1+1));
random6=(int)(1+Math.random()*(99-1+1));
}
System.out.println("The questions are as follows");
switch(random) {
case 1: System.out.println(random1+"+"+random2+"=?"); answer=random1 + random2; break;
case 2: System.out.println(random1+"-"+random2+"=?"); answer=random1 - random2; break;
case 3: System.out.println(random3+"*"+random4+"=?"); answer=random3 * random4; break;
case 4: System.out.println(random5+"/"+random6+"=?" ); answer=random5 / random6; break;
case 5: System.out.println(Frac1.toString()+"+"+Frac2.toString()+"=?");
_answer=Frac1.add(Frac2).toString(); break;
case 6: System.out.println(Frac1.toString()+"-"+Frac2.toString()+"=?");
_answer=Frac1.subtract(Frac2).toString(); break;
case 7: System.out.println(Frac1.toString()+"*"+Frac2.toString()+"=?");
_answer=Frac1.militiplicate(Frac2).toString(); break;
default: System.out.println(Frac1.toString()+" / "+Frac2.toString()+"=?");
_answer=Frac1.division(Frac2).toString(); break;
}
System.out.println("please input your anwer");
if(answer!=520) {
@SuppressWarnings("resource")
Scanner in =new Scanner(System.in);
int ans = in.nextInt();
System.out.println(answer);
System.out.println(ans);
if(ans==answer) {
System.out.println("congratulations!");
}else {
System.out.println("sorry,The corrent answer is "+answer);
}
}else {
@SuppressWarnings("resource")
Scanner in =new Scanner(System.in);
String _ans=in.next();
if(_ans.equals(_answer)) {
System.out.println("congratulations!!");
}else {
System.out.println("sorry,The corrent answer is "+_answer);
}
}
}
}
加入真分数的由于后面的修改没保存
加入了界面,javaswing的代码,这部分的代码有问题,能运行但功能未实现:
main类:
package homework01;
/***
* Because in the previous program has been achieved to determine the right or wrong,
* so only need to add the cumulative score and countdown function.
* @author 陈晨辰尘
*
*/
public class Operation02 {
public static void main(String[] args) throws InterruptedException {
Layout layout=new Layout();
layout.setS1("题目是");
CountDown countdown=new CountDown(60);
Thread countdownn=new Thread(countdown);
countdownn.start();
Subject subject=new Subject();
Thread subjectt=new Thread(subject);
subjectt.start();
layout.go();
}
}
swing界面类:
package homework01;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class Layout {
private JFrame frame;
private JTextField b1=new JTextField(10);
private JLabel s1 =new JLabel("题目是: ");
private JLabel s2 = new JLabel("s2");
private JLabel jsubject =new JLabel("subject");
private JLabel s3 =new JLabel("s3");
private JLabel s4 =new JLabel("s4");
public String getAnswer() {
String b=b1.getText();
return b;
}
public void setS1(String t1) {
s1.setText(t1);
}
public void setS2(String t2) {
s2.setText(t2);
}
public void setJsubjiect(String subject) {
jsubject.setText(subject);
}
public void setS3(String t3) {
s3.setText(t3);
}
public void setS4(String t4) {
s4.setText(t4);
}
public void go() throws InterruptedException {
frame = new JFrame("四则运算");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(3,2));
contentPane.add(s1);
contentPane.add(s2);
contentPane.add(jsubject);
contentPane.add(b1);
contentPane.add(s3);
contentPane.add(s4);
b1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Subject sub=new Subject();
sub.judge();
try {
Thread.sleep(3000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
sub.run();
}
});
frame.pack();
frame.setVisible(true);
}
}
倒计时类:
package homework01;
public class CountDown implements Runnable{
private int limitSec;
Layout layout=new Layout();
public CountDown(int limitsec){
this.limitSec = limitsec;
}
@Override
public void run() {
while(limitSec > 0){
String ms="倒计时:"+--limitSec+"秒";
layout.setS2(ms);
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
layout.setS2("时间到");
}
}
分数类:
package homework01;
public class Fraction {
private int molecular;
private int denominator;
public Fraction(int molecular,int denominator){
this.molecular=molecular;
this.denominator=denominator;
}
public int getGCD(int x,int y) {//获取最大公约数 x对应分子
while(x!=0&y!=0) {
y=y%x;
if(y==0) {
break;
}
x=x%y;
}
if(x==0) {
return y;
}else {
return x;
}
}
public String toString() {
if(molecular==denominator) {
return "1";
}
if(molecular==0) {
return "0";
}
return molecular+"/"+denominator;
}
public Fraction militiplicate(Fraction Frac) {
int gcd=getGCD(molecular*Frac.molecular,denominator*Frac.denominator);
Fraction Frac2=new Fraction(molecular*Frac.molecular/gcd,denominator*Frac.denominator/gcd);
return Frac2;
}
public Fraction division(Fraction Frac) {
int gcd=getGCD(molecular*Frac.denominator,denominator*Frac.molecular);
Fraction Frac2=new Fraction(molecular*Frac.denominator/gcd,denominator*Frac.molecular/gcd);
return Frac2;
}
public Fraction add(Fraction Frac) {
int x=molecular*Frac.denominator+Frac.molecular*denominator;
int y=denominator*Frac.denominator;
int gcd=getGCD(x,y);
Fraction Frac2=new Fraction(x/gcd,y/gcd);
return Frac2;
}
public Fraction subtract(Fraction Frac) {
int x=molecular*Frac.denominator- denominator*Frac.molecular;
int y=denominator*Frac.denominator;
int gcd;
if(x<0) {
int x1=0-x;
gcd=getGCD(x1,y);
}else {
gcd=getGCD(x,y);
}
Fraction Frac2=new Fraction(x/gcd,y/gcd);
return Frac2;
}
}
题目生成类:
package homework01;
public class Subject implements Runnable{
private static String _answer;
Layout layout=new Layout();
public void run() {
int random1=(int)(1+Math.random()*(99-1+1));
int random2=(int)(1+Math.random()*(99-1+1));
int random=(int)(1+Math.random()*(8-1+1));
int random3=(int)(1+Math.random()*(10-1+1));
int random4=(int)(1+Math.random()*(10-1+1));
int random5=(int)(1+Math.random()*(99-1+1));
int random6=(int)(1+Math.random()*(99-1+1));
int molecular=(int)(1+Math.random()*(9-1+1));
int denominator=(int)(1+Math.random()*(9-1+1));
while(molecular>=denominator) {
molecular=(int)(1+Math.random()*(9-1+1));
denominator=(int)(1+Math.random()*(9-1+1));
}
Fraction Frac1=new Fraction(molecular,denominator);
molecular=(int)(1+Math.random()*(9-1+1));
denominator=(int)(1+Math.random()*(9-1+1));
while(molecular>=denominator) {
molecular=(int)(1+Math.random()*(9-1+1));
denominator=(int)(1+Math.random()*(9-1+1));
}
Fraction Frac2=new Fraction(molecular,denominator);
while(random5%random6!=0) {//除法都是整除的
random5=(int)(1+Math.random()*(99-1+1));
random6=(int)(1+Math.random()*(99-1+1));
}
switch(random) {
case 1: layout.setJsubjiect(random1+"+"+random2+"=?"); _answer=random1 + random2+""; break;
case 2: layout.setJsubjiect(random1+"-"+random2+"=?"); _answer=random1 - random2+""; break;
case 3: layout.setJsubjiect(random3+"*"+random4+"=?"); _answer=random3 * random4+""; break;
case 4: layout.setJsubjiect(random5+"/"+random6+"=?" ); _answer=random5 / random6+""; break;
case 5: layout.setJsubjiect(Frac1.toString()+"+"+Frac2.toString()+"=?");
_answer=Frac1.add(Frac2).toString(); break;
case 6: layout.setJsubjiect(Frac1.toString()+"-"+Frac2.toString()+"=?");
_answer=Frac1.subtract(Frac2).toString(); break;
case 7: layout.setJsubjiect(Frac1.toString()+"*"+Frac2.toString()+"=?");
_answer=Frac1.militiplicate(Frac2).toString(); break;
default: layout.setJsubjiect(Frac1.toString()+" / "+Frac2.toString()+"=?");
_answer=Frac1.division(Frac2).toString(); break;
}
}
public void judge() {
String ans=layout.getAnswer();
int score=0;
if(ans.equals(_answer)) {
layout.setS3("congratulations!");
score++;
}else {
layout.setS3("sorry,The corrent answer is "+_answer);
}
}
}
能力有限,希望以后有时间回来修改。应该是线程问题,不知怎么解决。