Java最常用的6个简单的计算题

2025-05-29 0 39

1、3个白球 3个红球 6个黑球 随机拿出8个球,算出所有结果

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
public class Ball{

public static void main(String[] args){

int a=3,b=3,c=6,i=0;

for (int x=0;x<=a;x++){

for (int y=0;y<=b;y++){

for (int z=0;z<=c;z++){

if (x+y+z==8){

System.out.println("红球 " + x + "\\t白球 " + y + "\\t黑球 " + z );

i++;

}

}

}

}

System.out.println("有" + i + "结果");

}

}

2、数字金字塔

?

1

2

3

4

5

6

7

8

9

10
public class Pyramid {

public static void main(String args[]){

for (int i=1; i<=32; i=i*2) {

for (int k=1; k<=32/i; k=k*2)System.out.print("\\t");

for (int j=1; j<=i; j=j*2)System.out.print("\\t"+j);

for (int m=i/2; m>=1; m=m/2) System.out.print("\\t"+m);

System.out.print("\\n");

}

}

}

3、简单的判断日期格式是否正确

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16
import java.util.Scanner;

public class Date{

public static void main(String[] args) {

@SuppressWarnings("resource")//取消对input的警报

Scanner input=new Scanner(System.in);//声明扫描仪变量

System.out.println("请输入----年--月--日");//系统提示输入

int y = input.nextInt();

int m = input.nextInt();

int d = input.nextInt();

if (y>=1900&&y<=2050&&m>=1&&m<=12&&d>=1&&d<=31)

System.out.print("日期正确");

else

System.out.print("日期不正确");

}

}

4、计算1+2/3+3/5+4/7+5/9…的前20项的和

?

1

2

3

4

5

6

7

8
public class Num{

public static void main(String[] args) {

double sum=0;

for(int i=1;i<=10;i++)

sum=sum+i/(2.0*i-1);

System.out.println(sum);

}

}

5、给出本金,利率,年限计算存款(以函数的方式)

?

1

2

3

4

5

6

7

8

9

10

11

12
public class Bank {

public static double CBM(double money,double interest,int years){

for(int i=1;i<=years;i++){

money = money *(1+ interest);

}

return money;

}

public static void main(String[] args) {

System.out.println("300000元10年后的存款金额为"+CBM(300000,0.07,20));

System.out.println("200000元20年后的存款金额为"+CBM(200000,0.06,20));

}

}

6、计算五边形的面积。输入r,求面积s

?

1

2

3

4

5

6

7

8

9

10

11

12

13
import java.util.Scanner;

public class Circular{

public static void main(String[] args) {

@SuppressWarnings("resource")//取消对input的警报

Scanner input=new Scanner(System.in);//声明扫描仪变量

System.out.println("请输入五边形半径");//系统提示输入

double r = input.nextDouble();

double S;

S=5*(2*r*Math.sin(Math.PI/5)*(Math.pow(2*r*Math.sin(Math.PI/5), 2))/(4*Math.tan(Math.PI/5)));

System.out.println("五边形的面积为"+S);

}

}

原文链接:https://www.idaobin.com/archives/375.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java最常用的6个简单的计算题 https://www.kuaiidc.com/112350.html

相关文章

发表评论
暂无评论