日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

Java基础类库四则运算_00JAVA语法基础_四则运算 01

發布時間:2025/4/16 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java基础类库四则运算_00JAVA语法基础_四则运算 01 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

packageSizeyunsuan;/*** 30道100以內四則運算

*

**/

public classRandom {public static int getRandom( int n, intm)

{//產生n->m的隨機數

return (int) (Math.random() * (m - n) +n);

}public static chargetCharRandom()

{//隨機產生四種運算符

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); //產生1-100的隨機數

int y = (int) (Math.random() * (100 - 1 )+ 1); //產生1-100的隨機數

char sign =getCharRandom();/** 判斷乘法的范圍*/

switch(sign)

{case '+':

System.out.println("("+ (i+1) +")"+ x + " " + sign + " " + y + "=");

i++;break;case '-':if(x < y) //判斷減數與被減數的大小關系

{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的隨機數

y = (int) (Math.random() * (10 - 1 )+ 1);

System.out.println("("+ (i+1) +")"+ x + " " + sign + " " + y + "=");

i++;

};break;case '÷':do //循環生成除法

{

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的隨機數

y = (int) (Math.random() * (10 - 1 )+ 1);

System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );

i++;

}*/

/** 加減法判斷*/

/*if(sign == '+' || sign == '-')

{

if(x < y) //判斷減數與被減數的大小關系

{

int temp;

temp = x;

x = y;

y = temp;

}

System.out.println( "("+ (i+1) +")"+ x + " " + sign + " " + y + "=" );

i++;

}*/

/** 除法判斷*/

/*if(sign == '÷')

{

do //循環生成除法

{

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);

}

}

總結

以上是生活随笔為你收集整理的Java基础类库四则运算_00JAVA语法基础_四则运算 01的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。