Java面向对象程序设计董小园版课后习题答案参考(本人亲自编写,若有出入,欢迎互相讨论)第一章

Java面向对象程序设计董小园版课后习题答案参考

1、

import java.text.SimpleDateFormat;
import java.util.*;

//方法二:  只能进行当地时间的转换,交互性不好
public class Trans24212{
	public static void main(String[] args) {
		Date date=new Date();
		SimpleDateFormat sm=new SimpleDateFormat("hh:mm:ss");
		String formatedString=sm.format(date);
		System.out.println(formatedString);
	}
}


//方法一:
//public class Trans24212 {
//	public static void main(String[] args) {
//		System.out.println("请输入要转换24小时的时间:");
//		Scanner scanner = new Scanner(System.in);
//		String timetmp = scanner.next();
//		System.out.println("要转换的时间是:" + timetmp);
//		String num[] = timetmp.split(":");
//		int hour = Integer.parseInt(num[0]);
//		int minute = Integer.parseInt(num[1]);
//		int second = Integer.parseInt(num[2]);
//		if (!timetmp.contains(":")) {
//			System.out.print("输入格式不正确!");
//		} else if (num.length != 3)
//			System.out.print("输入格式不正确!");
//		else if (hour >= 0 && hour <= 12)
//			System.out.println("所输入时间是:" + timetmp);
//		else if (hour > 12 && hour < 24) {
//			if (minute <= 59 && minute >= 0 && second >= 0 && second <= 59) {
//				int hour1 = hour - 12;
//				if (minute == 0 || second == 0) {
//					String minute1 = "00";
//					String second1 = "00";
//					System.out.println("转换后的时间是:" + hour1 + ":" + minute1 + ":" + second1);
//				}
//				System.out.println("转换后的时间是:" + hour1 + ":" + minute + ":" + second);
//			} else if (minute > 59 || second > 59)
//				System.out.println("格式有误");
//		} else if (hour == 24) {
//			String hour2="00";
//			if (minute == 0 || second == 0) {
//				String minute1 = "00";
//				String second1 = "00";
//				System.out.println("转换后的时间是:" + hour2 + ":" + minute1 + ":" + second1);
//			}
//			System.out.println("转换后的时间是:" + hour2 + ":" + minute + ":" + second);
//		} else {
//			System.out.println("格式有误");
//		}
//		scanner.close();
////    SimpleDateFormat time=timet
//	}
//}

2、

import java.util.Scanner;

public class Printy {
	public static void main(String[] args) {
		System.out.println("请输入x的值:");
		Scanner sc = new Scanner(System.in);
		int x = sc.nextInt();
		GetY(x);
	}

	public static void GetY(int x) {
		int y;
		if (x < 0) {
			y = x;
		} else if (x >= 1 && x < 10) {
			y = 2 * x - 1;
		} else {
			y = 3 * x - 11;
		}
		System.out.println("y="+y);
	}
}

3、

import java.util.Scanner;

public class ScoreParse {
	public static void main(String[] args) {
		System.out.println("请输入学生的成绩:");
		Scanner scanner = new Scanner(System.in);
		int score = scanner.nextInt();
		int num = score / 10;
		switch (num) {
		case 9:
			if (score >= 90)
			System.out.println("成绩为A");
			break;

		case 8:
			if (score >= 80 && score <= 89)
			System.out.println("成绩为B");
			break;
		case 7:
			if (score >= 70 && score <= 79)
			System.out.println("成绩为C");
			break;
		case 6:
			if (score >= 60 && score <= 69)
			System.out.println("成绩为D");
			break;
		case 5:
		case 4:
		case 3:
		case 2:
		case 1:
			if (score < 60)
			System.out.println("成绩为E");
			break;
		default:
			System.out.println("输入成绩不合法!");
			break;
		}
		scanner.close();
	}
}

4、

import java.util.Scanner;

//假设今天是周日,求n天后是星期几,用switch语句实现,输入n的值
public class WhichDay {
	public static void main(String[] args) {
		System.out.println("请输入n的数值:");
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		int day = n % 7;
		switch (day) {
		case 0:
			System.out.println("今天是星期日");
			break;
		case 1:
			System.out.println("今天是星期一");
			break;
		case 2:
			System.out.println("今天是星期二");
			break;
		case 3:
			System.out.println("今天是星期三");
			break;
		case 4:
			System.out.println("今天是星期四");
			break;
		case 5:
			System.out.println("今天是星期五");
			break;
		case 6:
			System.out.println("今天是星期六");
			break;

		}
		scanner.close();
	}
}

5、

import java.util.Scanner;

//输入一个数字,打印相应个数的星号
public class PrintOfNumsStars {
	public static void main(String[] args) {
		System.out.println("请输入要输入的星星个数:");
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
//		方法一:for循环
//		for (int i = 0; i < num; i++) {
//			System.out.print("*");
//		}
		// 方法二:while
		int i = 0;
		while (i < num) {
			System.out.print("*");
			i++;
		}
		scanner.close();
	}
}

6、

//编写程序,求0-100之间的偶数和
public class Sum02100 {
	public static void main(String[] args) {
		int sum = 0;
		int i = 0;

//		for语句
//		for (int i = 0; i <= 100; i++) {
//			if (i % 2 == 0) {
//				sum += i;
//			}
//		}

//		do...while语句
//		int i =2;
//		do {
//				sum += i;
//				i+=2;
//			
//		} while (i <=100);

//		while语句
//两种方法:1-------------
//		int i=2;
//		while (i <= 100) {
//				sum += i;
//				i+=2;
//		}
//		int sum = 0;
//		2---------------
		while (i <= 100) {
			if (i % 2 == 0) {
				sum += i++;
			} else {
				i++;
			}
		}
		System.out.println("0-100的偶数和为:" + sum);
		//学习笔记:
	/*
	 * 注意:不能写成
	 * while (i <= 100) {
			if (i % 2 == 0) {
				sum += i;
				i++;
				}
			}
			这样i=0时,可以执行if语句,当i++后,i=1,i不能满足进入if条件,则i永远等于1,
			进入无限循环中。必须加个while的出口	即,i不满足条件时用了else来帮助i达到条件。
			最终循环完毕。
	 */
	}
}

7、

import java.util.Scanner;

//求一到输入的一个大于1的整数的阶乘,如:5!=1x2x3x4x5
public class JiEChen {
	public static void main(String[] args) {
		System.out.println("请输入大于1的整数:");
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		int results = 1;

		// for循环
//		for (int i = 1; i < num; ) {
//			results *= ++i;
//		}

		// while循环
//		int i = 1;
//		while (i < num) {
//			i++;
//			results = i * results;
//		}

		// do...while循环
		int i = 1;
		do {
			++i;
			results *= i;

		} while (i < num);

		System.out.println("该整数的阶乘是:" + results);
		scanner.close();
		/*
		 * 学习笔记: 注意do...while 和while的区别,do..while先执行一次,注意把控两者出口的控制条件 do { ++i; results
		 * *= i;
		 * 
		 * } while (i <= num);得到结果为5的阶乘,++i或者i++在results前面时,while语句应为i
	}
}

8、

public class NineNineForm {
	public static void main(String[] args) {
		for (int i = 1; i <= 9; i++) {// 行
			for (int j = 1; j <= i; j++) {// 列
						System.out.print(j + " x " + i + " = " + i * j+"  ");
						if (j==i) {
							System.out.println();
						}
				}
		}
	}
}


你可能感兴趣的:(java学习)