四則運算程式
- 一、運行流程
- 二、程式代碼部分
一、運行流程
- 程式分為兩個部分,主程式和四則運算程式,
- 命令列運行方式:
- javac Main.java 這是第一個程式編譯
- javac Cal.java這是第二個程式編譯
- 然后運行,java Main 30// 30為要生成的題目數,你可以換成其他的


二、程式代碼部分
- 這是main函式,保存至Main.java檔案夾即可
import java.io.PrintStream;
public class Main{
public static void main(String[] args){
int n=Integer.parseInt(args[0]);// 命令列輸入引數
if(n<1||n>1000){
System.out.println("輸入失敗,請輸入小于1000的正整數!");
}else{
System.out.println("輸入成功!");
try {
PrintStream ps = new PrintStream("../result.txt");//可以自己添加存放該檔案的路徑,要用/或者\\來區分層級
System.setOut(ps);
}catch(Exception e){
System.out.println("創建result檔案失敗!");
}
System.out.println("小學森,就決定是你了");
Cal.QuesFunc(n);
}
}
}
- 這是四則運算程式,保存至Cal.java即可
import java.util.Random;
public class Cal {
public static void Integer(){
String arith1 = null;//運算式
char[]optCom = {'+','-','*','÷'}; //加減乘除操作集
Random random = new Random();
int optIdx = random.nextInt(4);
int sum1 = 0;//每步運算結果
int optNum = random.nextInt(3) + 3; //3-5個運算子
int a = random.nextInt(100);//隨機生成0-100內的整數a
int b = random.nextInt(100);//隨機生成0-100內的整數b
if(optCom[optIdx] == '+') sum1 = a + b;
if(optCom[optIdx] == '*') sum1 = a * b;
if(optCom[optIdx] == '-'){//若a-b為負數,則重新隨機產生a b
while(a-b < 0)
{
a = random.nextInt(100);
b = random.nextInt(100);
}
sum1 = a - b;
}
if(optCom[optIdx] == '÷'){//若a÷b不能整除,則重新隨機產生a b
if (b == 0)
b = random.nextInt(100);
while (a%b != 0) {
a = random.nextInt(100);
b = random.nextInt(100);
}
sum1 = a/b;
}
arith1 = a + "" + optCom[optIdx] + "" + b;//將算式更新
for(int j = 1; j < optNum; j++){//隨機生成余下的運算子
int optIdx1 = random.nextInt(4);//隨機生成下一個運算子號
int c = random.nextInt(100);//隨機生成0-100內的整數c
if(optCom[optIdx1] == '+'){
sum1 += c;
arith1 = arith1 + "" + optCom[optIdx1] + "" + c;
}
if(optCom[optIdx1] == '-'){
while(sum1-c<0)
{
c=random.nextInt(100);
}
sum1 += c;
arith1 = arith1 + "" + optCom[optIdx1] + "" + c;
}
if(optCom[optIdx1] == '*'){//若下一個運算子號為乘號,判斷前后兩個運算子的優先級
if(optCom[optIdx] == '+' || optCom[optIdx] == '-')
{
arith1 = "(" + arith1 + ")" + optCom[optIdx1] + c;
}
else
{
arith1 = arith1 + "" + optCom[optIdx1] + "" + c;
}
sum1 = sum1*c;
}
if(optCom[optIdx1] == '÷'){//若下一個運算子號為除號,判斷前后兩個運算子的優先級
while (c == 0 || sum1%c != 0) {
c=random.nextInt(100);
}
if(optCom[optIdx] == '+' || optCom[optIdx] == '-')
{
arith1= "(" + arith1 + ")" + optCom[optIdx1] + c;
}
else
{
arith1 = arith1 + "" + optCom[optIdx1] + "" + c;
}
sum1 = sum1/c;
}
// optIdx = optIdx1;//更新運算子
}
System.out.println(arith1 + "=" + sum1);//輸出運算式及結果
}
public static void Fraction(){
String arith2 = null;//運算式
//String sum2 = null;
char[]optSim = {'+','-'}; //分數加減操作集
Random random = new Random();
int mole = 0;
int deno = 0; //初始化分子分母
int optNum = random.nextInt(3) + 3; //3-5個運算子
int mole1 = random.nextInt(20)+1;//隨機生成分子1
int deno1 = random.nextInt(20)+1;//隨機生成分母1
if (mole1 != 0 && deno1 != 0) {
if (mole1 > deno1) {// 如果分子大于分母,也就是不是真分數時,交換分子分母,使其變成真分數
int temp = mole1;
mole1 = deno1;
deno1 = temp;
}
if (mole1 == deno1) {// 如果分子剛好等于分母,重新生成分子
mole1 = random.nextInt(20);
}
int gcd1 = gcd(mole1, deno1);// 求分子分母最大公因數,保證分數形式最簡
deno1 = deno1 / gcd1;// 化簡
mole1 = mole1 / gcd1;// 化簡
}
arith2 = mole1 + "/" + deno1;// 存盤題目
for (int k = 0; k < optNum; k++) {// 小于運算子數量時不斷產生分數,不斷計算
int deno2 = random.nextInt(20);// 生成分母
int mole2 = random.nextInt(20);// 生成分子
if (mole2 != 0 && deno2 != 0) {
if (mole2 > deno2) {// 避免不是真分數
int temp = mole2;
mole2 = deno2;
deno2 = temp;
}
if (mole2 == deno2) {// 如果分子等于分母,重新生成分子
mole2 = random.nextInt(20);
}
int gcd2 = gcd(mole2, deno2);// 化簡分式,使其最簡
deno2 = deno2 / gcd2;
mole2 = mole2 / gcd2;
}
int idx = random.nextInt(2);//隨機生成運算子下標
if (optSim[idx] == '+') {// 如果是加號,實作分數加法
if (deno1 == deno2) {// 如果兩個分母相同,直接將分子相加
mole = mole1 + mole2;
} else {// 通分,相加
deno = deno1 * deno2;
mole = mole1 * deno2 + mole2 * deno1;
}
if (mole > deno) {// 如果運算結果不是真分數
k--;// 計數的u減一,也就是重新生成重新計算
} else {// 在給定范圍內的話,通分運算結果
int gcd = gcd(mole, deno);
deno = deno / gcd;
mole = mole / gcd;
arith2 = arith2 + optSim[idx] + mole2 + "/" + deno2;
deno1 = deno;// 儲存通分結果
mole1 = mole;
}
} else {// 如果是減號,實作減法操作
if (deno1 == deno2) {// 分母相同直接分子相減
mole = mole1 - mole2;
} else {// 其他情況,先通分再相減
deno = deno1 * deno2;
mole = mole1 * deno2 - mole2 * deno1;
}
if (mole < 0) {// 如果導致結果小于0了,就重新生成
k--;
} else {// 通分結果化簡
int gcd = gcd(mole, deno);
deno = deno / gcd;
mole = mole / gcd;
arith2 = arith2 + optSim[idx] + mole2 + "/" + deno2;
deno1 = deno;// 儲存通分結果
mole1 = mole;
}
}
}
System.out.println(arith2 + " = " + mole + "/" + deno);// 輸出題目和答案
}
//最大公因數,每次代入,顯然有a<b
public static int gcd(int a, int b){
while(a!=0)
return gcd(b % a, a);
return b;
}
public static void QuesFunc(int n) {// 實作產生n個混合四則運算的方法
Random random = new Random();
for (int i = 0; i < n; i++) {
int flag = random.nextInt(4); //隨機生成整數四則運算或真分數加減運算
if (flag == 0 || flag == 2) {//0生成整數四則運算
Integer();
} else {//執行真分數加減運算
Fraction();
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/276692.html
標籤:其他
上一篇:用陣列寫三子棋游戲
