標籤:
設計思想: 整數運算 使用random()函數隨機產生兩個0到100的整數,用0到3數字代表加減乘除運算子,用random()隨機產生代表運算子的數字,如果是除法,還需考慮第二個數是否為0,如果是則重新進行一次隨機數產生,迴圈減一,最後輸出。
真分數運算 使用random()函數隨機產生四個0到100的整數,用0到3數字代表加減乘除運算子,用random()隨機產生代表運算子的數字,需考慮第二個數和第四個數是否為0以及第一個數是否大於第二個數,第三個數是否大於第四個數,如果是則重新進行一次隨機數產生,迴圈減一,最後輸出。
來源程式代碼:
import java.util.Scanner;
public class A{
public static void main(String []args){
int a,b,c,n=0;
Scanner reader=new Scanner(System.in);
System.out.print("整數運算請選 1,真分數運算請選 2:");
n=reader.nextInt();
if(n==1)
{
for(int i=1;i<=30;i++)
{
a=(int)(Math.random()*100+0);
b=(int)(Math.random()*100+0);
c=(int)(Math.random()*4+0);
switch(c)
{case 0:
System.out.print(a+"+"+b+"="+"\t");
break;
case 1:
System.out.print(a+"-"+b+"="+"\t");
break;
case 2:
System.out.print(a+"*"+b+"="+"\t");
break;
case 3:
if(b==0)
{i--;break;}
else
System.out.print(a+"/"+b+"="+"\t");
break;
}
if(i%5==0)
{System.out.print("\n");}
}
}
if(n==2)
{
int d,e,f,g,h;
for(int i=1;i<=30;i++)
{
d=(int)(Math.random()*100+0);
e=(int)(Math.random()*100+0);
f=(int)(Math.random()*100+0);
g=(int)(Math.random()*100+0);
h=(int)(Math.random()*4+0);
switch(h)
{
case 0:
if(e==0||h==0||d>e||f>g)
{i--;break;}
else
System.out.println("("+d+"/"+e+")"+"+"+"("+f+"/"+g+")"+"="+"\t\t");
break;
case 1:
if(e==0||h==0||d>e||f>g)
{i--;break;}
else
{System.out.println("("+d+"/"+e+")"+"-"+"("+f+"/"+g+")"+"="+"\t\t");
break;}
case 2:
if(e==0||h==0||d>e||f>g)
{i--;break;}
else
{System.out.println("("+d+"/"+e+")"+"*"+"("+f+"/"+g+")"+"="+"\t\t");
break;}
case 3:
if(e==0||h==0||d>e||f>g)
{i--;break;}
else
{ System.out.println("("+d+"/"+e+")"+"/"+"("+f+"/"+g+")"+"="+"\t\t");
break;}
}
}
}
}
}
運行結果:
軟體工程個人作業01