Java 基礎編程練習


轉自作者soulsjie。http://www.cnblogs.com/soulsjie/p/7501097.html

1、編寫程序實現對給定的 4 個整數從大到小的順序排列。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package HomeWork01;
import java.util.Scanner;
 
public class HomeWork01 {
     static int number= 4 ;        //輸入4個數存放在數組中                   
     static int [] t1 = new int [number];           
     public static void main(String[] args) {
         HomeWork01 jiejie= new HomeWork01();
         jiejie.shunxun();
     }
     void shunxun(){
                 System.out.println( "請輸入4個數:" );
                 Scanner in_t1 = new Scanner(System.in); //循環輸入數組
                 for ( int i= 0 ;i<number;i++){
                     t1[i]=in_t1.nextInt();}       
                 for ( int i = 0 ; i < t1.length; i++) {
                     int pos = i;
                     for ( int j = i + 1 ; j < t1.length; j++) {
                         if (t1[pos] > t1[j])
                             pos = j;
                     }
                     if (pos != i) {
                         t1[i] = t1[i] + t1[pos];
                         t1[pos] = t1[i] - t1[pos];
                         t1[i] = t1[i] - t1[pos];
                     }
                 }
                     
                 for ( int i = t1.length - 1 ; i >= 0 ; i--)
                     System.out.print(t1[i] + "\t" );
     }
}

 

2、編寫程序求一元二次方程的根。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package HomeWork02;
import java.util.Scanner;
public class HomeWork02
//△=b^2-4ac的值,若△小於0,一元二次方程無根.若△等於0,一元二次方程有兩個相等的根.若△大於0,一元二次方程有兩個不相等的實數根
     {
     public static void main(String [] args){
     Scanner sc = new Scanner(System.in);
     System.out.println( "輸入2次方的系數" );
     int a = sc.nextInt();
     System.out.println( "輸入1次方的系數" );
     int b = sc.nextInt();
     System.out.println( "輸入0次方的系數" );
     int c = sc.nextInt();
     if ((b*b - 4 *a*c)< 0 ){     //  判斷方程是否有解
         System.out.println( "方程無解!" );
         return ;
     }
     else {
         System.out.println( "方程有解!" );
     }
     double x1 = (-b + Math.sqrt(b*b - 4 *a*c))/ 2 *a;
     double x2 = (-b - Math.sqrt(b*b - 4 *a*c))/ 2 *a;
     System.out.println( "根分別是 " + x1 + "\t" + x2);
     }
}

 

 

3、編寫程序,輸入一個字符,判斷它是否為小寫字母,如果是,將它轉換成大

寫字母,否則,不轉換。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package HomeWork03;
import java.util.Scanner;
public class HomeWork03 {
     public static void main(String[] args) {
         //小寫字母的ascll值為97-122
         //大寫字母的ascll值為65-90
         System.out.println( "請輸入一個字母:\n" );
         Scanner input = new Scanner(System.in);
         char zimu=input.next().charAt( 0 );
           if (zimu>= 97 &&zimu<= 122 ){           //判斷是否是小寫字母
               System.err.println( "該字母是小寫字母" );
               zimu=( char ) (zimu- 32 );        //如果是小寫字母則 將其轉換成大寫字母
               System.err.println( "轉換之后的大寫字母是:" +zimu);
           }
           else {
              System.out.println( "該字母不是小寫字母!" );          
           }
     }  
}

 

4、輸入 3 個正數,判斷能否構成一個三角形。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package HomeWork04;
import java.util.Scanner;
 
public class HomeWork04 {
     public static void main(String [] args){
     int a;
     int b;
     int c;
     System.out.println( "請輸入三個正整數:" );
     Scanner in= new Scanner(System.in);
     a=in.nextInt();
     b=in.nextInt();
     c=in.nextInt();
     
     if (a<= 0 ||b<= 0 ||c<= 0 )
     {
         System.out.println( "輸入的必須是正整數!" );
     }
     if ((a+b)>c&&(a+c)>b&&(b+c)>a)
     {
         System.out.println( "能構成三角形!" );
     }
     else {
         System.out.println( "不能構成三角形!" );
     }
     }
     }

 

5、編寫程序,對輸入的年、月、日,給出該天是該年的第多少天?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package HomeWork05;
import java.util.Scanner;
public class HomeWork05 {
public static void main(String[]args){
     Scanner sc = new Scanner(System.in);
     System.out.print( "年" );
     int year=sc.nextInt();
     System.out.print( "月" );
     int month=sc.nextInt();
     System.out.print( "日" );
     int day=sc.nextInt();
     int days= 0 ;
     switch (month){
         case 12 :days+= 30 ;
         case 11 :days+= 31 ;
         case 10 :days+= 30 ;
         case 9 :days+= 31 ;
         case 8 :days+= 31 ;
         case 7 :days+= 30 ;
         case 6 :days+= 31 ;
         case 5 :days+= 30 ;
         case 4 :days+= 31 ;
         case 3 :
             if ((year% 4 == 0 &&year% 100 != 0 )||(year% 400 == 0 )){
                 days+= 29 ;
                 }
             else {
                 days+= 28 ;
                 }
         case 2 :days+= 31 ;
         case 1 :days+=day;
     }
         System.out.print( "第" + days + "天" );
}
}

 

6、編寫程序,從鍵盤輸入一個 0~99999 之間的任意數,判斷輸入的數是幾位

數?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package HomeWork06;
import java.util.Scanner;
public class HomeWork06 {
public static void main(String[]args){
     
     Scanner sc = new Scanner(System.in);
     System.out.print( "請輸入一個0~99999 之間的任意數" );
     int number=sc.nextInt();
     if (number/ 10000 >= 1 &&number/ 10000 < 10 ){
         System.out.println(number+ "\t是5位數" );
     }
     else if (number/ 1000 >= 1 ){
         System.out.println(number+ "\t是4位數" );
     }
     else if (number/ 100 >= 1 ){
         System.out.println(number+ "\t是3位數" );
     }
     else if (number/ 10 >= 1 ){
         System.out.println(number+ "\t是2位數" );
     }
     else if (number/ 1 >= 1 ){
         System.out.println(number+ "\t是1位數" );
     }
     }
}

 

7、編寫程序,給定一個學生成績,給出相應等級:

90~100 優秀

80~89 良好

70~79 中等

60~69 及格

0~59 不及格

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package HomeWork07;
import java.util.Scanner;
public class HomeWork07 {
 
     public static void main(String[] args) {
         HomeWork07 jiejie= new HomeWork07();
         jiejie.chengjie();
 
     }
     void chengjie(){
         Scanner sc = new Scanner(System.in);
         System.out.println( "請輸入學生成績:" );
         int a = sc.nextInt();
         if (a>= 90 &&a<= 100 ){
             System.out.println( "該學生的成績是" +a+ "\t成績優秀" );
         }
         else if (a>= 80 &&a< 90 ){
             System.out.println( "該學生的成績是" +a+ "\t成績良好" );
         }
         else if (a>= 70 &&a< 80 ){
             System.out.println( "該學生的成績是" +a+ "\t成績中等" );
         }
         else if (a>= 60 &&a< 70 ){
             System.out.println( "該學生的成績是" +a+ "\t成績及格" );
         }
         else {
             System.out.println( "該學生的成績是" +a+ "\t成績不及格" );
         }
     }
 
}

 

8、編寫程序,對輸入的一個整數,按相反順序輸出該數。例如,輸入為 3578,

輸出為 8753。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package HomeWork08;
import java.util.Scanner;
 
public class HomeWork08 {
 
     public static void main(String[]args){
         Scanner sc = new Scanner(System.in);
         System.out.println( "請輸入一個整數:" );
         int read = sc.nextInt();
         //方法一     reverse()API
         System.out.println( "方法一:" );
         StringBuilder sb  =  new StringBuilder(String.valueOf(read));
         System.out.println(sb.reverse());
         //方法二   將字符串轉換成字符數組,反序輸出
         String str= read + "" ;
         char fuzu[]=str.toCharArray();
         String temp= "" ;
         for ( int a=fuzu.length- 1 ;a>= 0 ;a--){
             temp=temp+fuzu[a];
         }
         System.out.println( "方法二:" );
         System.out.println(temp);
         
}
}

 

9、用 while 循環,計算 1~200 之間所有 3 的倍數之和。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package HomeWork09;
 
public class HomeWork09 {
 
     public static void main(String[] args) {
         // 用while循環,計算1~200之間所有3的倍數之和。
         int a= 1 ;
         int sum= 0 ;
         while (a<= 200 ){
             if (a% 3 == 0 ){
                 sum=sum+a;
             }
             a++;
         }
         System.out.println( "1~200之間所有3的倍數之和為:" +sum);
     }
}

 

10、編寫程序,輸出 200~500 之間的所有素數。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package HomeWork10;
public class HomeWork10 {
     public static void main(String[] args) {
         int num= 200 ;
         while (num<= 500 ) {
             boolean tag= true ;       //素數標記
             for ( int d= 2 ;d<=num- 1 ;d++){
                 if (num % d== 0 ){
                     tag= false ;
                     break ;
                 }
             }
             if (tag){                //如果是素數
                 System.out.println(num);
             }
             num++;
         }
     }
}

 

11、編寫程序解決“百錢買百雞”問題。公雞五錢一只,母雞三錢一只,小雞

一錢三只,現有百錢欲買百雞,共有多少種買法?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package HomeWork11;
public class HomeWork11 {
     public static void main(String[] args) {
         /* 、編寫程序解決“百錢買百雞”問題。
          * 公雞五錢一只,母雞三錢一只,
          * 小雞 一錢三只,
          * 現有百錢欲買百雞,共有多少種買法? */
         for ( int g= 0 ;g<= 20 ;g++){
             for ( int m= 0 ;m<= 33 ;m++){
                 for ( int x= 0 ;x<= 100 -g-m;x++){
                     if (x % 3 == 0 && 5 *g+m* 3 +x/ 3 == 100 && g+m+x == 100 ){
                         System.out.println( "公雞" +g+ "只母雞" +m+ "只小雞" +x+ "只" );
                     }
                 }
             }
         }
     }
}

 

12、使用循環語句輸出下面的圖形。

#

# # #

# # # # #

# # # # # # #

# # # # # # # # #

?
1
2
3
4
5
6
7
8
9
10
11
12
13
package HomeWork12;
public class HomeWork12 {
     public static void main(String[] args) {
         int aa=- 1 ;
         for ( int a= 0 ;a< 5 ;a++){
             aa+= 2 ;
             for ( int b= 1 ;b<=aa;b++){ 
                 System.out.print( "#"  );
             }
             System.out.println();}
         
     }
}

 

13、驗證“鬼谷猜想”:對任意自然數,若是奇數,就對它乘以 3 再加 1;若是

偶數,就對它除以 2,這樣得到一個新數,再按上述計算規則進行計算,一直進

行下去,最終必然得到 1。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package HomeWork13;
import java.util.Random;;
public class HomeWork13 {
     public static void main(String[] args) {
         /*
          * 驗證“鬼谷猜想”:對任意自然數,若是奇數,
          * 就對它乘以3再加1;若是 偶數,就對它除以2,
          * 這樣得到一個新數,
          * 再按上述計算規則進行計算,
          * 一直進 行下去,最終必然得到1。 */
         int num;
         Random rd= new Random();
         //Integer.MAX_VALUE為最大的整數
         num= 1 +rd.nextInt(Integer.MAX_VALUE); //產生數的范圍-2[31]----2[31]-1
         //System.err.println(rd.nextInt(100));//產生數>=0且<100
         System.out.println( "原本的數為" +num);
         while (num!= 1 ){
             System.out.println( "產生的新數是" +num);
             if (num% 2 == 0 ){
                 //偶數
                 num=num/ 2 ;
             }
             else {
                 num=num* 3 + 1 ;
             }
         }
         System.out.println(num);
     }
}

 

14、編程求 1~10000 之間的所有“完全數”,完全數是該數的所有因子之和等於該數的數。例如,6 的因子有 1、2、3,且 6=1+2+3,所以 6 是完全數。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package HomeWork14;
 
public class HomeWork14 {
     public static boolean isyinzi( int num ){
         int sum= 0 ;
         //判斷一個整數是不是一個完全數
         for ( int d=num- 1 ;d>= 1 ;d--){
             if (num%d== 0 ){
                 sum+=d;
             }
         }
         return sum==num;
     }
 
     public static void main(String[] args) {
         // TODO Auto-generated method stub
         /*
          * 編程求1~10000之間的所有“完全數”,
          * 完全數是該數的所有因子之和等
          * 於該數的數。例如,6的因子有1、2、3,
          * 且6=1+2+3,所以6是完全數*/
         for ( int a= 1 ;a<= 1000 ;a++){
             int num=a;
             if (isyinzi(num)){
                 System.out.println(num);
             }
         }
 
     }
 
}

 

 

15、一個整數的各位數字之和能被 9 整除,則該數也能被 9 整除。編程驗證給

定的整數能否被 9 整除。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package HomeWork15;
import java.util.Random;
public class HomeWork15 {
     public static void panduan( int num){
         int b=num/ 100 ;      //百位數
         int s=num% 100 / 10 ;   //十位數
         int g=num% 10 ;       //個位數
         if (num% 9 == 0 ){
             System.out.println(num+ "能被9整除" );
             if ((b+s+g)% 9 == 0 ){
                 System.out.println( "同時" +num+ "的各個位數之和也能被9整除" );
             }
             else {
                 System.out.println( "但是" +num+ "的各個位數之和不能被9整除" );
             }
         }
         else
             System.out.println( "next test!" );
         
     }
     public static void main(String[] args) {
         Random rd= new Random();
         int shu= 10 +rd.nextInt( 90 );
         shu =shu * 9 ;
         panduan(shu);
     }
}

 

16、猴子吃桃問題。猴子第一天摘下若干個桃子,當時就吃了一半,還不過癮,

就又吃了一個。第二天又將剩下的桃子吃掉一半,又多吃了一個。以后每天都吃

前一天剩下的一半零一個。到第 10 天在想吃的時候就剩一個桃子了,求第一天共

摘下來多少個桃子?

?
1
2
3
4
5
6
7
8
9
10
11
12
package HomeWork16;
public class HomeWork16 {
public static void main(String[] args) {
     int total= 1 ;
     int day= 10 ;
     for ( int i = 10 ; i > 0 ; i--) {
         System.out.println( "第" +day+ "天,有桃子" +total+ "顆" );
         total=(total+ 1 )* 2 ;
         day--; 
     }
}
}

 

17、水仙花數是指一個 n 位數 ( n≥3 ),它的每個位上的數字的 n 次冪之和等

於它本身。(例如:1^3 + 5^3 + 3^3 = 153)。編程求出所有三位的水仙花數。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package HomeWork17;
public class HomeWork17 {
     public static void main(String[] args) {
         for ( int num= 100 ;num< 1000 ;num++){
             if (isshuixian(num)){
                 System.out.println(num);
             }
         }
     }
     //判斷一個數是不是水仙花數
     public static boolean isshuixian( int num){
         int b=num/ 100 ;
         int s=num% 100 / 10 ;
         int g=num% 10 ;
         return Math.pow(b, 3 )
                 +Math.pow(s, 3 )
                 +Math.pow(g, 3 )==num? true : false ;
     }
}

 

18、已知 XYZ+YZZ=532,其中,X、Y、Z 為數字,編程求出 X、Y 和 Z 的值。

19、古典問題:有一對兔子,從出生后第 3 個月起每個月都生一對兔子,小兔

子長到第三個月后每個月又生一對兔子,假如兔子都不死,問每個月的兔子總數

為多少?

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package HomeWork19;
 
import java.util.Scanner;
 
public class HomeWork19 {
     /*
      * 古典問題:有一對兔子,
      * 從出生后第3 個月起每個月都生一對兔子,
      * 小兔 子長到第三個月后每個月又生一對兔子,
      * 假如兔子都不死,
      * 問每個月的兔子總數 為多少? */
     public int rubbit( int mon){
         if (mon<= 2 ){
             return 1 ;
         }
         else {
             return rubbit(mon- 1 )+rubbit(mon- 2 );
         }
     }
     public static void main(String[] args) {
         int r= 1 ;
         int rr= 1 ;
         int rrr= 1 ;
         System.out.println( "方法一:" );
         for ( int a= 1 ;a<= 12 ;a++){
             //12個月
             if (a<= 2 ){
                 r= 1 ;
             }
             else {
                 //當前月等於前兩個月之和
                 r=rr+rrr;
                 rrr=rr;
                 rr=r;
             }
             System.out.println(r* 2 );
         }
         System.out.println( "方法二,求指定月份的兔子數量:" );
         HomeWork19 jisuan= new  HomeWork19();
         System.out.println( "請輸入月份:" );
         Scanner sc= new Scanner(System.in);
         int yue=sc.nextInt();
         System.out.println(yue+ "月份的兔子數量是" +(jisuan.rubbit(yue))* 2 );
     }
}

 

20、將一個正整數分解質因數。例如:輸入 90,打印出 90=2*3*3*5。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package HomeWork20;
import java.util.Scanner;
public class HomeWork20 {
     public static void main(String[] args) {
         System.out.println( "請輸入一個整數:" );
         Scanner sc= new Scanner(System.in);
         int num=sc.nextInt();
         System.out.println(num+ "的質因數有:" );
         for ( int i= 2 ;i<num;i++){
             while (num%i== 0 ){
                 num/=i;
                 System.out.print(i+ " " );
             }
         }
         System.out.print( "  " +num);
     }
 
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM