java基础练习 字符串,控制流,日历,日期等

1,对基本控制流程的一些练习

  1 package org.base.practice3;
  2 
  3 import org.junit.Test;
  4 
  5 /**
  6  * Created with IntelliJ IDEA.
  7  * User: cutter.li
  8  * Date: 14-3-10
  9  * Time: 上午10:14
 10  * java基础练习题第三章
 11  */
 12 public class PractiseTest {
 13 
 14 
 15     @Test
 16     public void exercise1() {
 17         char x = '你', y = 'e', z = '吃';
 18 
 19         if (x > 'A') {
 20             y = '爱';
 21             z = '情';
 22         } else {
 23             y = '我';
 24         }
 25         z = '她';
 26         System.out.println(" " + x + y + z);
 27 
 28     }
 29 
 30     @Test
 31     public void exercise2() {
 32         char c = '';
 33         for (int i = 1; i <= 4; i++) {
 34             switch (i) {
 35                 case 1:
 36                     c = 'b';
 37                     System.out.print(c);
 38                 case 2:
 39                     c = 'e';
 40                     System.out.print(c);
 41                     break;
 42                 case 3:
 43                     c = 'p';
 44                     System.out.print(c);
 45                 default:
 46                     System.out.print("!");
 47             }
 48         }
 49     }
 50 
 51     //编写一个程序求1!+2!+3!+...+10!
 52     @Test
 53     public void exercise3() {
 54 
 55         int sum = 0;
 56 
 57         for (int i = 1; i <= 10; i++) {
 58             int factorial = 1;
 59             for (int k = 1; k <= i; k++) {
 60                 factorial *= k;
 61             }
 62             System.out.println(i + "的阶乘是:" + factorial);
 63             sum += factorial;
 64         }
 65         System.out.println("**********************1到10的阶乘之和是:" + sum);
 66     }
 67 
 68     //求100以内的素数(素数指在大于1的自然数中,除了1和此整数自身外,无法被其他自然数整除的数。)
 69     @Test
 70     public void exercise4() {
 71 
 72         for (int i = 1; i <= 100; i++) {
 73 
 74             boolean isPrime = true;
 75             for (int k = 2; k <= i; k++) {
 76                 if (i % k == 0 && i != k) {
 77                     isPrime = false;
 78                     break;
 79                 } else {
 80                     continue;
 81                 }
 82 
 83             }
 84             if (isPrime && i > 1) {
 85                 System.out.print(i + " , ");
 86             }
 87         }
 88     }
 89 
 90     //分别用do-while和for计算出1+1/2!+1/3!+...的前20项之和
 91     @Test
 92     public void exercise5() {
 93         int i = 1;
 94         float sum = 0;
 95         do {
 96             int factorial = 1;
 97             for (int k = 1; k <= i; k++) {
 98                 factorial *= k;
 99             }
100             sum += (float) 1 / factorial;
101             i++;
102         } while (i <= 20);
103         System.out.println("do-while方法计算的前20项的和是:" + sum);
104 
105 
106         float he = 0;
107         for (int j = 1; j <= 20; j++) {
108             int jiecheng = 1;
109             for (int m = 1; m <= j; m++) {
110                 jiecheng *= m;
111             }
112             he += (float) 1 / jiecheng;
113         }
114         System.out.println("for方法计算的前20项的和是:" + he);
115     }
116 
117     //求1000以内的完数(一个数如果恰好等于他的除了本身之外的所有因子数之和,这个数称为完数)
118     @Test
119     public void exercise6() {
120 
121         for (int i = 1; i <= 1000; i++) {
122             int sum = 0;
123             for (int k = 1; k <= i; k++) {
124                 if (i % k == 0 && k < i) {
125                     sum += k;
126                 }
127             }
128             if (sum == i) {
129                 System.out.print(i + " , ");
130             }
131         }
132     }
133 
134     //分别使用while和for计算出8+88+888+...前10项的和
135     @Test
136     public void exercise7() {
137 
138         int sum = 0;
139 
140         for (int i = 1; i <= 10; i++) {
141             int num = 0;
142             for (int k = 1; k <= i; k++) {
143                 num += Math.pow(10, k - 1);
144 
145             }
146             System.out.println("第" + i + "的值是:" + num);
147             sum += 8 * num;
148         }
149 
150         System.out.println("前10项的和是:" + sum);
151     }
152 
153     //计算出1+2+3+...+n<8888 的最大正整数n
154     @Test
155     public void exercise8() {
156 
157         int sum = 0;
158         for (int i = 1; i <= Integer.MAX_VALUE; i++) {
159             sum += i;
160             if (sum >= 8888) {
161                 System.out.println("最大的整数是:" + (i - 1));
162                 break;
163             }
164         }
165 
166     }
167 
168 }

2,对基本类工具Date,Calendar,BigInterger,Math的练习

  1 package org.base.practice6;
  2 
  3 import org.junit.Test;
  4 
  5 import java.math.BigInteger;
  6 import java.text.SimpleDateFormat;
  7 import java.util.Calendar;
  8 import java.util.Date;
  9 
 10 /**
 11  * Created with IntelliJ IDEA.
 12  * User: cutter.li
 13  * Date: 14-3-10
 14  * Time: 下午12:00
 15  * java基础知识第六章练习题
 16  */
 17 public class PractiseTest {
 18 
 19     //用date的不带参数的构造函数创建日期,输出格式为 星期 小时 分 秒
 20     @Test
 21     public void exercise1() {
 22 
 23         Date now = new Date();
 24         String nowStr = new SimpleDateFormat("E HH mm ss").format(now);
 25 
 26         System.out.println("当前的时间:" + nowStr);
 27 
 28 
 29     }
 30 
 31     //输入2006年2月的日历页面,程序要处理闰年的问题
 32     @Test
 33     public void exercise2() {
 34 
 35         Calendar calendar = Calendar.getInstance();
 36         calendar.set(2006, 2, 1);
 37 
 38         int year = calendar.get(Calendar.YEAR);
 39         System.out.println(year + "年" + Calendar.MONTH + "月");
 40         System.out.println("日 一 二 三 四 五 六");
 41 
 42         int weeks = calendar.get(Calendar.DAY_OF_WEEK);
 43 
 44         int dayOfMonth = 28;
 45         if (year % 400 == 0 || (year % 4 == 0 && year % 100 > 0)) {
 46             dayOfMonth = 29;
 47         }
 48         int week = calendar.get(Calendar.DAY_OF_WEEK);
 49         for (int k = 1; k < week; k++) {
 50             System.out.print("*  ");
 51         }
 52         for (int i = 1; i <= dayOfMonth; i++) {
 53 
 54             System.out.print(i + "  ");
 55             if (week % 7 == 0) {
 56                 System.out.println();
 57             }
 58             week++;
 59 
 60         }
 61 
 62     }
 63     @Test
 64     public void exercise3() {
 65 
 66         Calendar calendar1=Calendar.getInstance();
 67         calendar1.set(1988,1,2);
 68 
 69         Calendar calendar2=Calendar.getInstance();
 70        calendar2.setTime(new Date());
 71 
 72         long days= calendar2.getTimeInMillis()-calendar1.getTimeInMillis();
 73 
 74         System.out.println("我活了"+days/(24*60*60*1000)+"天");
 75 
 76 
 77     }
 78 
 79     @Test
 80     public void exercise4() {
 81           System.out.println(Math.nextUp(100f));
 82     }
 83     @Test
 84     public void exercise5() {
 85 
 86         BigInteger sum = BigInteger.ZERO;
 87         for (BigInteger i = new BigInteger("1", 10); i.intValue() <= 30; i=i.add(new BigInteger("1"))) {
 88             BigInteger factorial = new BigInteger("1");
 89             for (BigInteger k = new BigInteger("1"); k.intValue() <= i.intValue();k= k.add(new BigInteger("1"))) {
 90                 factorial = factorial.multiply(k);
 91             }
 92             System.out.println(i+"的阶乘是:" + factorial);
 93             sum = sum.add(factorial);
 94         }
 95         System.out.println("1!+2!+...+30!的和是:" + sum);
 96 
 97 
 98     }
 99 
100 }
View Code

3,对字符串的一些练习

 1 package org.base.practice5;
 2 
 3 import org.junit.Test;
 4 
 5 import java.util.Arrays;
 6 
 7 /**
 8  * Created with IntelliJ IDEA.
 9  * User: cutter.li
10  * Date: 14-3-10
11  * Time: 下午2:33
12  * 字符串相关练习题
13  */
14 public class PractiseTest {
15 
16     @Test
17     public void exercise1()
18     {
19         String str="I am a 7Road employee !";
20 
21         System.out.println(str);
22 
23         System.out.println(str.toLowerCase());
24 
25         System.out.println(str.toUpperCase());
26 
27     }
28 
29     @Test
30     public void exercise2()
31     {
32         String str="cutter GG :".concat("do u want to find a job ?");
33 
34         System.out.println(str);
35 
36         System.out.println(29&51+((29^51)>>1));
37 
38 
39     }
40 
41     @Test
42     public void exercise3()
43     {
44        String str="中国科学技术大学";
45         char a=str.charAt(2),b=str.charAt(6);
46 
47         System.out.println(a+" , "+b);
48     }
49 
50     @Test
51     public void exercise4()
52     {
53        int a[]={465,2,7979,12,9,3,9655,-10};
54 
55         Arrays.sort(a);
56 
57         for(int i:a)
58         {
59             System.out.print(i+" , ");
60         }
61 
62         System.out.println();
63         int b[]= Arrays.copyOf(a,20);
64 
65         for(int i:b)
66         {
67             System.out.print(i + " , ");
68         }
69     }
70 
71 
72     @Test
73     public void exercise5()
74     {
75         Object[] a=new Object[10];
76        System.arraycopy(new Object[]{"a",0,"cdef",'f'},0,a,0,2);
77 
78         for(Object i:a)
79         {
80             System.out.print(i + " , ");
81         }
82     }
83 }

以上练习使用的是junit4.11,为求简单,使用的是命令行输出,为了复习基本的java知识,特找了java2使用教程的练习来练手,看一遍书,温故而知新···

原文地址:https://www.cnblogs.com/snidget/p/3592288.html