Java基础类库四则运算_00JAVA语法基础_四则运算 01
packageSizeyunsuan;/*** 30道100以內(nèi)四則運算
*
**/
public classRandom {public static int getRandom( int n, intm)
{//產(chǎn)生n->m的隨機數(shù)
return (int) (Math.random() * (m - n) +n);
}public static chargetCharRandom()
{//隨機產(chǎn)生四種運算符
char sign = 0;intSn;
Sn= getRandom(1,5);switch(Sn)
{case 1:sign = '+' ;break;case 2:sign = '-' ;break;case 3:sign = '×' ;break;case 4:sign = '÷' ;break;
}returnsign;
}public static voidmain(String[] args) {//TODO Auto-generated method stub
int i = 0;do{int x = (int) (Math.random() * (100 - 1 )+ 1); //產(chǎn)生1-100的隨機數(shù)
int y = (int) (Math.random() * (100 - 1 )+ 1); //產(chǎn)生1-100的隨機數(shù)
char sign =getCharRandom();/** 判斷乘法的范圍*/
switch(sign)
{case '+':
System.out.println("("+ (i+1) +")"+ x + " " + sign + " " + y + "=");
i++;break;case '-':if(x < y) //判斷減數(shù)與被減數(shù)的大小關(guān)系
{inttemp;
temp=x;
x=y;
y=temp;
}
System.out.println("("+ (i+1) +")"+ x + " " + sign + " " + y + "=");
i++;break;case '×':
{
x= (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的隨機數(shù)
y = (int) (Math.random() * (10 - 1 )+ 1);
System.out.println("("+ (i+1) +")"+ x + " " + sign + " " + y + "=");
i++;
};break;case '÷':do //循環(huán)生成除法
{
y= (int) (Math.random() * (10 - 1 )+ 1);
x= (int) (Math.random() * (9*y - 1 )+ 1);
}while(x % y != 0) ;
System.out.println("("+ (i+1) +")"+ x + " " + sign + " " + y + "=");
i++;break;
}/*if(sign == '×')
{
x = (int) (Math.random() * (10 - 1 )+ 1);//新生成x,y<9的隨機數(shù)
y = (int) (Math.random() * (10 - 1 )+ 1);
System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );
i++;
}*/
/** 加減法判斷*/
/*if(sign == '+' || sign == '-')
{
if(x < y) //判斷減數(shù)與被減數(shù)的大小關(guān)系
{
int temp;
temp = x;
x = y;
y = temp;
}
System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );
i++;
}*/
/** 除法判斷*/
/*if(sign == '÷')
{
do //循環(huán)生成除法
{
y = (int) (Math.random() * (10 - 1 )+ 1);
x = (int) (Math.random() * (9*y - 1 )+ 1);
}
while(x % y != 0) ;
System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );
i++;
}*/}while(i<30);
}
}
總結(jié)
以上是生活随笔為你收集整理的Java基础类库四则运算_00JAVA语法基础_四则运算 01的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java调用net_Java调用C#.n
- 下一篇: 用java做出32选7_用java做的一