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

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

 package org.base.practice3;

 import org.junit.Test;

 /**
* Created with IntelliJ IDEA.
* User: cutter.li
* Date: 14-3-10
* Time: 上午10:14
* java基础练习题第三章
*/
public class PractiseTest { @Test
public void exercise1() {
char x = '你', y = 'e', z = '吃'; if (x > 'A') {
y = '爱';
z = '情';
} else {
y = '我';
}
z = '她';
System.out.println(" " + x + y + z); } @Test
public void exercise2() {
char c = '\0';
for (int i = 1; i <= 4; i++) {
switch (i) {
case 1:
c = 'b';
System.out.print(c);
case 2:
c = 'e';
System.out.print(c);
break;
case 3:
c = 'p';
System.out.print(c);
default:
System.out.print("!");
}
}
} //编写一个程序求1!+2!+3!+...+10!
@Test
public void exercise3() { int sum = 0; for (int i = 1; i <= 10; i++) {
int factorial = 1;
for (int k = 1; k <= i; k++) {
factorial *= k;
}
System.out.println(i + "的阶乘是:" + factorial);
sum += factorial;
}
System.out.println("**********************1到10的阶乘之和是:" + sum);
} //求100以内的素数(素数指在大于1的自然数中,除了1和此整数自身外,无法被其他自然数整除的数。)
@Test
public void exercise4() { for (int i = 1; i <= 100; i++) { boolean isPrime = true;
for (int k = 2; k <= i; k++) {
if (i % k == 0 && i != k) {
isPrime = false;
break;
} else {
continue;
} }
if (isPrime && i > 1) {
System.out.print(i + " , ");
}
}
} //分别用do-while和for计算出1+1/2!+1/3!+...的前20项之和
@Test
public void exercise5() {
int i = 1;
float sum = 0;
do {
int factorial = 1;
for (int k = 1; k <= i; k++) {
factorial *= k;
}
sum += (float) 1 / factorial;
i++;
} while (i <= 20);
System.out.println("do-while方法计算的前20项的和是:" + sum); float he = 0;
for (int j = 1; j <= 20; j++) {
int jiecheng = 1;
for (int m = 1; m <= j; m++) {
jiecheng *= m;
}
he += (float) 1 / jiecheng;
}
System.out.println("for方法计算的前20项的和是:" + he);
} //求1000以内的完数(一个数如果恰好等于他的除了本身之外的所有因子数之和,这个数称为完数)
@Test
public void exercise6() { for (int i = 1; i <= 1000; i++) {
int sum = 0;
for (int k = 1; k <= i; k++) {
if (i % k == 0 && k < i) {
sum += k;
}
}
if (sum == i) {
System.out.print(i + " , ");
}
}
} //分别使用while和for计算出8+88+888+...前10项的和
@Test
public void exercise7() { int sum = 0; for (int i = 1; i <= 10; i++) {
int num = 0;
for (int k = 1; k <= i; k++) {
num += Math.pow(10, k - 1); }
System.out.println("第" + i + "的值是:" + num);
sum += 8 * num;
} System.out.println("前10项的和是:" + sum);
} //计算出1+2+3+...+n<8888 的最大正整数n
@Test
public void exercise8() { int sum = 0;
for (int i = 1; i <= Integer.MAX_VALUE; i++) {
sum += i;
if (sum >= 8888) {
System.out.println("最大的整数是:" + (i - 1));
break;
}
} } }

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

 package org.base.practice6;

 import org.junit.Test;

 import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date; /**
* Created with IntelliJ IDEA.
* User: cutter.li
* Date: 14-3-10
* Time: 下午12:00
* java基础知识第六章练习题
*/
public class PractiseTest { //用date的不带参数的构造函数创建日期,输出格式为 星期 小时 分 秒
@Test
public void exercise1() { Date now = new Date();
String nowStr = new SimpleDateFormat("E HH mm ss").format(now); System.out.println("当前的时间:" + nowStr); } //输入2006年2月的日历页面,程序要处理闰年的问题
@Test
public void exercise2() { Calendar calendar = Calendar.getInstance();
calendar.set(2006, 2, 1); int year = calendar.get(Calendar.YEAR);
System.out.println(year + "年" + Calendar.MONTH + "月");
System.out.println("日 一 二 三 四 五 六"); int weeks = calendar.get(Calendar.DAY_OF_WEEK); int dayOfMonth = 28;
if (year % 400 == 0 || (year % 4 == 0 && year % 100 > 0)) {
dayOfMonth = 29;
}
int week = calendar.get(Calendar.DAY_OF_WEEK);
for (int k = 1; k < week; k++) {
System.out.print("* ");
}
for (int i = 1; i <= dayOfMonth; i++) { System.out.print(i + " ");
if (week % 7 == 0) {
System.out.println();
}
week++; } }
@Test
public void exercise3() { Calendar calendar1=Calendar.getInstance();
calendar1.set(1988,1,2); Calendar calendar2=Calendar.getInstance();
calendar2.setTime(new Date()); long days= calendar2.getTimeInMillis()-calendar1.getTimeInMillis(); System.out.println("我活了"+days/(24*60*60*1000)+"天"); } @Test
public void exercise4() {
System.out.println(Math.nextUp(100f));
}
@Test
public void exercise5() { BigInteger sum = BigInteger.ZERO;
for (BigInteger i = new BigInteger("1", 10); i.intValue() <= 30; i=i.add(new BigInteger("1"))) {
BigInteger factorial = new BigInteger("1");
for (BigInteger k = new BigInteger("1"); k.intValue() <= i.intValue();k= k.add(new BigInteger("1"))) {
factorial = factorial.multiply(k);
}
System.out.println(i+"的阶乘是:" + factorial);
sum = sum.add(factorial);
}
System.out.println("1!+2!+...+30!的和是:" + sum); } }

3,对字符串的一些练习

 package org.base.practice5;

 import org.junit.Test;

 import java.util.Arrays;

 /**
* Created with IntelliJ IDEA.
* User: cutter.li
* Date: 14-3-10
* Time: 下午2:33
* 字符串相关练习题
*/
public class PractiseTest { @Test
public void exercise1()
{
String str="I am a 7Road employee !"; System.out.println(str); System.out.println(str.toLowerCase()); System.out.println(str.toUpperCase()); } @Test
public void exercise2()
{
String str="cutter GG :".concat("do u want to find a job ?"); System.out.println(str); System.out.println(29&51+((29^51)>>1)); } @Test
public void exercise3()
{
String str="中国科学技术大学";
char a=str.charAt(2),b=str.charAt(6); System.out.println(a+" , "+b);
} @Test
public void exercise4()
{
int a[]={465,2,7979,12,9,3,9655,-10}; Arrays.sort(a); for(int i:a)
{
System.out.print(i+" , ");
} System.out.println();
int b[]= Arrays.copyOf(a,20); for(int i:b)
{
System.out.print(i + " , ");
}
} @Test
public void exercise5()
{
Object[] a=new Object[10];
System.arraycopy(new Object[]{"a",0,"cdef",'f'},0,a,0,2); for(Object i:a)
{
System.out.print(i + " , ");
}
}
}

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

上一篇:iOS开发-canOpenURL: failed for URL: "xx" - error:"This app is not allowed to query for scheme xx"


下一篇:Java 配置maven及阿里云镜像