《Head First Java》第一章练习

P.20

public class Shuffle1 {
    public static void main(String[] args) {
        int x = 3;
        while (x > 0) {
            if (x > 2) {
                System.out.print("a");
            }
            x = x - 1;
            System.out.print("-");
            if (x == 2) {
                System.out.print("b c");
            }
            x = x - 1;
            System.out.print("-");
            if (x == 1) {
                System.out.print("d");
                x = x - 1;
            }
        }
    }
}

p.21

A

public class Exercise1b {
    public static void main(String[] args) {
        int x = 1;
        while(x < 10){
            if(x>3){
                System.out.println("big x");
            }
            //要使x的值随着循环而减少,避免死循环,x每次循环减少多少,自己定!
            //但要注意的是编译可以通过
            x--;
        }
    }
}

B

//public calss Exercise1b{ 类没了,当然得出问题,并且编译不会通过
public static void main(String[]args) {
		int x = 5;
		while(x > 1) {
			x = x - 1;
			if(x < 3) {
				System.out.println("small x");
			}
		}
	}	
//}

C

public class Exercise1b {
//书的本章刚讲:main方法相当与程序的起点,且一个程序有且只有一个
//当我们学到后面也能知道:一个模块(功能)中,也有且只有一个main方法
//	public static void main(String[]args) {  
		int x = 5;
		while(x > 1) {
			x = x - 1;
			if(x < 3) {
				System.out.println("small x");
		
			}
		}	
//	}	
}

P.22

JavaCross7.0icon-default.png?t=M276https://blog.csdn.net/u012814856/article/details/78852471?utm_medium=distribute.pc_relevant.none-task-blog-2~default~baidujs_utm_term~default-0.pc_relevant_default&spm=1001.2101.3001.4242.1&utm_relevant_index=3

P.23

y = x - y;00 11 21 32 42
y = y + x;00 11 23 36 410

y = y + 2;

if(y > 4){

     y = y - 2;

}

02 14 25 36 47

x = x + 1;

y = y + x;

11 34 59

if(y<5){

   x = x + 1;

   if(y < 3){

        x = x -1;

   }

}

y = y + 2;

02 14 36 48

P.24

public class PoolPuzzleOne {
    public static void main(String[] args) {
        int x = 0;
        while (x < 4) {
            System.out.print("a");
            if (x < 1) {
                System.out.print(" ");
            }
            System.out.print("n");
            if (x > 1) {
                System.out.print(" oyster");
                x = x + 2;
            }
            if (x == 1) {
                System.out.print("noys");
            }
            if (x < 1) {
                System.out.print("oise");
            }
            System.out.println("");
            x = x + 1;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值