Java編寫一個隨機產生小學生四則運算題30道


//注:這個程序還沒有實現的地方為分數的計算方法未能實現,只是簡單的兩個數運算,沒有實現多個數,四則運算中的數沒有涉及0.
package 課堂測試1;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
public class Arithmetic {
    String f()
        {
            int i=(int)(1+Math.random()*100);
            int j=(int)(1+Math.random()*100);
            if(i>=j)
            {
                int temp=i;
                i=j;
                j=temp;
            }
            return("("+i+"/"+j+")");
        }//Math.random()是令系統隨機選取大於等於 0.0 且小於 1.0 的偽隨機 double 值
    /*
     * Java中Math類的random()方法可以生成[0,1)之間的隨機浮點數。而double類型數據強制轉換成int類型,整數部分賦值給int類型變量,小數點之后的小數部分將會丟失。如果要生成[0,n]的隨機整數的話,只需要Math.random()乘以n+1,生成[0,n+1)的浮點數,再強制類型轉換為int類型,只取其整數部分,即可得到[0,n]的整數。
int b=(int)(Math.random()*10);//生成[0,9]之間的隨機整數。
int temp=m+(int)(Math.random()*(n+1-m)); //生成從m到n的隨機整數[m,n]
     int  num = (int)(Math.random()*2+1)
//以上代碼即設置一個隨機1到3(取不到3)的變量num。
     //產生一個[0,1)之間的隨機數。
Math.random():
返回指定范圍的隨機數(m-n之間)的公式:
Math.random()*(n-m)+m;
或者
Math.random()*(n+1-m)+m*/
   public static void main(String[] args)
        {
       try {
           File file=new File("E:\\result.txt");
           if(file.exists()) {file.delete();}
       FileWriter fw=new FileWriter(file,true);
       PrintWriter pw=new PrintWriter(fw);
       //PrintWriter()的作用是為了定義流輸出的位置,並且此流可以正常的存儲中文,減少亂碼輸出。
       //備注:文件流用完之后必須及時通過close方法關閉,否則會一直處於打開狀態,直至程序停止,增加系統負擔。
      double result;
           String a,b;
            Arithmetic lianxi=new Arithmetic();
            for(int n=0;n<30;n++)
            {System.out.println("第"+(n+1)+"道");
                a=lianxi.f();
                b=lianxi.f();
                int i=(int)(1+Math.random()*100);
                int j=(int)(1+Math.random()*100);
                double i1=i;
                double j1=j;
                String[]operator={"+","-","*","/"};
                Random r=new Random();
                int num=r.nextInt(4);//該方法的作用是生成一個隨機的int值,該值介於[0,4)的區間,也就是0到4之間的隨機int值,包含0而不包含4
         int t=(int)(Math.random()*3);//分為三種情況,兩個整數運算,一個整數一個分數運算,兩個分數運算
      //兩個整數運算
         if(t==0) {
            if(operator[num]=="/") {
                if(j==0) {
                    while(j==0)
                        j= (int)(Math.random()*100);
                }
            }//考慮除數是否為0的情況,不過用在這邊沒有意義,這里的j不可能為0
            String str1=i+operator[num]+j;
        if(operator[num]=="+") {result=i+j;
        System.out.println(str1+"=");
        pw.println(str1+"="+result);//保存到文件中去
        }
        else if(operator[num]=="-") {
            result=i-j;
            System.out.println(str1+"=");
            pw.println(str1+"="+result);
        }
        else if(operator[num]=="*") {
            result=i*j;
            System.out.println(str1+"=");
            pw.println(str1+"="+result);
        }
        else if(operator[num]=="/") {
            result=i1/j1;
            System.out.println(str1+"=");
            pw.println(str1+"="+result);
        }
        }
         //一個整數一個分數運算
        else if(t==1) {
            if(operator[num]=="/") {
                if(j==0) {
                    while(j==0)
                        j= (int)(Math.random()*100);
                }
            }
            String str2=a+operator[num]+j;
             if(operator[num]=="+") {
            System.out.println(str2+"=");
            pw.println(str2+"=");
        }
             if(operator[num]=="-") {
                 System.out.println(str2+"=");
                 pw.println(str2+"=");
             } 
             if(operator[num]=="*") {
                 System.out.println(str2+"=");
                 pw.println(str2+"=");
             }
             if(operator[num]=="/") {
                 System.out.println(str2+"=");
                 pw.println(str2+"=");
             }
        }
         //兩個分數運算
        else if(t==2) {
            String str3=a+operator[num]+b;
        if(operator[num]=="+") {
            System.out.println(str3+"=");
            pw.println(str3+"=");
        }
             if(operator[num]=="-") {
                 System.out.println(str3+"=");
                 pw.println(str3+"=");
             } 
             if(operator[num]=="*") {
                 System.out.println(str3+"=");
                 pw.println(str3+"=");
             }
             if(operator[num]=="/") {
                 System.out.println(str3+"=");
                 pw.println(str3+"=");
             }
        }
            }
            pw.flush();
            pw.close();
            fw.close();
       }catch(IOException e) {
           e.printStackTrace();
       }
        }
}
/*參考鏈接
 https://blog.csdn.net/qq_36868342/article/details/73478112
 https://blog.csdn.net/qq_21808961/article/details/79931087
 https://www.cnblogs.com/xiaotiaosi/p/6394147.html
 https://zhidao.baidu.com/question/417476227.html?word=printwriter&ms=1&rid=9823837345489847717
https://blog.csdn.net/duncandavid/article/details/60871080        
*/     
        

    

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM