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