Java_continue/return/习题

continue        继续, con全部 tin拿住 + ue,全部拿住,做事拿到尾,继续。

return 

 当return用在方法时,表示退出该方法,用在main时,表示退出程序。

这和C也是一致的,不过在C中不叫方法,叫函数。(这句话不太确定)

public class HW01 {
	public static void main(String[] args) {
		double money = 100000;
		int count = 0;
		while (money > 0) {
			if (money > 50000) {
				money *= 0.95;
			} else {
				money -= 1000;
			}
			++count;
		}
		System.out.println("次数是 " + count);
	}
}

这题我欠考虑了,当钱<1000时,不能交1000元了。

 改

public class HW01 {
	public static void main(String[] args) {
		double money = 100000;
		int count = 0;
		while (money >= 1000) {
			if (money > 50000) {
				money *= 0.95;
			} else {
				money -= 1000;
			}
			++count;
		}
		System.out.println("次数是 " + count);
	}
}

money >= 1000的时候才考虑交钱。

我的

import java.util.Scanner;

public class HW04 {
	public static void main(String[] args) {
		Scanner myScanner = new Scanner(System.in);
		int num = myScanner.nextInt();
		int temp = num;
		int sum = 0;
		int n = 0;
		while (temp != 0) {
			n = temp % 10;
			sum += n * n * n;
			temp /= 10;
		}
		if (sum == num) {
			System.out.println("是水仙花数");
		} else {
			System.out.println("不是水仙花数");
		}
	}
}

 老师

import java.util.Scanner;

public class HW04 {
	public static void main(String[] args) {
		Scanner myScanner = new Scanner(System.in);
		int num = myScanner.nextInt();
		int n1 = num % 10;
		int n2 = num % 100 / 10;
		int n3 = num / 100;
		if (n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3 == num) {
			System.out.println("Y");
		} else {
			System.out.println("N");
		}
	}
}

思路最重要!

public class HW06 {
	public static void main(String[] args) {
		int count = 0;
		for (int i = 1; i <= 100; ++i) {
			if (i % 5 != 0) {
				System.out.print(i + "\t");
				++count;
				if (count % 5 == 0)
					System.out.println();
			}
		}
	}
}

注意这个i + '\t'和i + "\t"的区别

i + '\t'是i的值和\t的unicode码相加等到的数字,而""引起来的才是字符串连接

 我的

 

public class HW07 {
	public static void main(String[] args) {
		for (int i = 0; i <= 25; ++i) {
			System.out.print((char)(97 + i) + " ");
			System.out.println((char)(65 + i) + " ");
		}
	}
}

老师

public class HW07 {
	public static void main(String[] args) {
		for (char c = 'a'; c <= 'z'; ++c) {
			System.out.print(c + " ");
		}
		System.out.println();
		for (char c = 'Z'; c >= 'A'; --c) {
			System.out.print(c + " ");
		}
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值