软件构造的作业保存

每个算式的类

class test  
{
    private int fir;    
    private int sec;    
    private char op;    
    private int result; 
    public test(){}
    public test(int fir, int sec, char op)
    {
        this.fir = fir;
        this.sec = sec;
        this.op = op;
    }
    public int getFir(){
        return this.fir;
    }
    public int getSec(){
        return this.sec;
    }
    public char getop(){
        return this.op;
    }
    public void setFir(int num){
        this.fir = num;
    }
    public void setSec(int num){
        this.sec = num;
    }
    public void setOp(char ch){
        this.op = ch;
    }
    public int result(){
        if(op == '+'){
            return this.result = (fir + sec);    
        }else{
            return this.result = (fir - sec);
        }
    }
    public boolean Check(int fir, int sec ,char op){
        if(op == '+'){
            if(fir + sec > 100)
                return false;
        }else{
            if(fir - sec < 0)
                return false;
        }
        return true;
    }
}

主函数代码

import java.util.Random;
import java.util.Scanner;
public class Sag{
	public static void main(String[]args){
		int fir, sec;
		char op;
		Random random = new Random();
		//要吧这些地方换成是 那个test类(等到自己的电脑再去修改)这边表示的是算式的产生 
		for(int i = 1 ; i <= 10 ; i ++){
			fir = (int)random.nextInt(101);
			sec = (int)random.nextInt(101);
			int tmp = (int)random.nextInt(2);
			op = (tmp == 1 ? '+':'-');
			int result = 0;
			Scanner sc = new Scanner(System.in);
			if(op == '+')
			{
				System.out.printf("%02d + %02d = ",fir ,sec);
				result = sc.nextInt();
			}
			else
			{
				System.out.printf("%02d + %02d = ",fir ,sec);
				result = sc.nextInt();
			}
			System.out.println();
		}
	}
}	



猜你喜欢

转载自blog.csdn.net/galesaur_wcy/article/details/83988749
今日推荐