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