学通JAVA_WEB的24堂课课后答案大题代码.doc
文本预览下载声明
Web课后代码:
2.1
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println(请输入一个年份:);
long year = scan.nextLong();// 接收用户输入
if (year % 4 == 0 year % 100 != 0 || year % 400 == 0) { // 是闰年
System.out.print(year + 是闰年!);
} else { // 不是闰年
System.out.print(year + 不是闰年!);}}}
public class Factorial {
public static void main(String[] args) {
int n = 16;
long result = 1;
if ((n 0) || (n 17)) {
System.out.println(n的取值范围是0至17,大于17会超出long类型范围);
} else if (n == 0) {
System.out.println(0的阶乘等于1);
} else {
for (int i = n; i 0; i--) {
result *= i;
}
System.out.println(n + 的阶乘等于: + result);}}}
public class Book {
private String name = Java Web开发实战宝典;
private int price = 89;
public void printInfo(){
System.out.println(图书的名称为:+name);
System.out.println(图书的价格为:+price+元);}
public static void main(String[] args) {
Book book = new Book();
book.printInfo();}}
2.4
public class Test {
public static void main(String[] args) {
for(int i=1;i=9;i++){// 循环控制变量从1遍历到9
for(int j=1;j=i;j++){// 第二层循环控制变量与第一层最大索引相等
// 输出计算结果但不换行
System.out.print(j+*+i+=+i*j+\t);}
System.out.println();// 在外层循环中换行 }}}
public class Student {
static int speak(int m) throws MyException{ //定义方法抛出异常
if(m 1000){ //判断参数是否小于0
throw new MyException(参数大于1000); //异常信息
} return m; //返回值 }
public static void main(String args[]){ //主方法
try{ //try语句包含可能发生异常的语句
int result = speak(1050); //调用方法quotient( }
catch (MyException e) { //处理自定义异常
e.printStackTrace();
System.out.println(e.getMessage()); //输出异常信息}
catch (ArithmeticException e) { //处理ArithmeticException异常
System.out.println(参数大于1000); //输出提示信息 }
catch (Exception e) {
显示全部