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