一、设计思想
利用随机数获取运算的数据和运算的符号,进行加和输出运算。
二、源程序代码
import java.util.Random;
import java.util.Scanner;
public class Yunsuan {
int x;
int y;
int z;
int sum;
char c;
String string;
String[] s= {"+","-","*","/"};
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getC() {
Random random = new Random();
int n=random.nextInt(4);
if(n<=1) {
return s[random.nextInt(s.length)];
}
else {
return s[random.nextInt(s.length)];
}
}
public String getString() {
return string;
}
public void setString(String string) {
this.string = string;
}
public void setSum(int sum) {
this.sum = sum;
}
public int Getsum() {
return sum;
}
public void random() { //灏佽�鐢熸垚鐨勯殢鏈烘暟
Random r=new Random();
x=r.nextInt(99);
y=r.nextInt(99);
z=r.nextInt(4);
switch(getC()) {
case "+":
sum=x+y;
break;
case "-":
sum=x-y;
break;
case "*":
if(x>10||y>10)
{
random();
}
else
{
sum=x*y;
}
break;
case "/":
if(y==0) //闄ゆ暟涓�0閲嶆柊璋冪敤
{
random();
}
else
{
sum=x/y;
}
break;
}
setX(x);
setY(y);
setSum(sum);
}
public void yunsuan(){
//random(); //璋冪敤鐢熸垚闅忔満杩愮畻
random();
/*if(sum<100||sum>0)
{ //鍜屽ぇ浜�100閲嶆柊鐢熸垚
random();
}
else
{
yunsuan();
}*/
String a=x+" "+c+" "+y;
setString(a);
if(c=='/')
{
System.out.println(x+" "+getC()+" "+y+"="+x+""+c+""+y);
}
else {
System.out.println(x+" "+c+" "+y+"="+sum);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Yunsuan yunsuan=new Yunsuan();
int n;
Scanner in=new Scanner(System.in);
n=in.nextInt();
for(int i=0;i<n;i++ )
{
yunsuan.yunsuan();
}
}
}
三、运行结果