【韩顺平零基础学java】第五章课后题

在这里插入图片描述
在这里插入图片描述

  • 1

public class Homework01 { 

	//编写一个main方法
	public static void main(String[] args) {
		/*
		某人有100,000元,每经过一次路口,需要交费,规则如下:
		1) 当现金>50000时,每次交5%
		2) 当现金<=50000时,每次交1000
		编程计算该人可以经过多少次路口, 要求: 使用 while + break方式完成

		思路分析
		1. 定义 double money 保存 100000
		2. 根据题的要求,我们分析出来有三种情况 
			money > 50000 
			money >=1000 && money <= 50000
			money < 1000 
		3. 使用多分支 if-elseif-else 
		4. while+break[money < 1000], 同时使用一个变量count来保存通过路口
		代码实现
		 */
		double money = 100000;//还有多少钱
		int count = 0; //累积过的路口
		while(true) { //无限循环
			if(money > 50000) { //过路口
				//money = money -  money * 0.05;
				money *= 0.95; //过了这个路口后,还有这么多钱
				count++;
			} else if(money >=1000) {
				money -= 1000;
				count++;
			} else { //钱不够1000
				break;
			}
		}
		System.out.println("100000 可以过 " + count + " 路口..");
	}
}
  • 2

public class Homework02 { 

	//编写一个main方法
	public static void main(String[] args) {
		int n = 22;
	
			if(n > 0) { 
				System.out.println(n+ "大于0");

			} else if(n =0) {
				System.out.println(n+ "等于0");

			} else { //钱不够1000
				System.out.println(n+ "小于0");
			}
		}
	}
  • 3
import java.util.Scanner;


public class ProcessControl {

	public static void main(String[] args) {	
		//判断闰年
		
		//1.能被4整除,但不能被100整除,就是闰年;
		//2.能被400整除,也是闰年
		
		Scanner in = new Scanner(System.in);
		System.out.println("请输入年份:");
		int year = in.nextInt();
		
		if( (year%4==0 && year%100 !=0) || year%400==0 ) {
		       System.out.println(year+"是闰年");
		}else {
				System.out.println(year+"不是闰年");
		}
	}
}

  • 4
public class Homework04 { 

	//编写一个main方法
	public static void main(String[] args) {

		/*
		4. 判断一个整数是否是水仙花数,所谓水仙花数是指一个3位数,
		其各个位上数字立方和等于其本身。
		例如: 153 = 1*1*1 + 3*3*3 + 5*5*5

		思路分析 => 多听. 见多识广..
		1. 比如 int n = 153; 
		2. 先得到 n的百位,十位 ,各位的数字, 使用 if 判断他们的立方和是否相等
		3. n的百位 = n / 100
		4. n的十位 = n % 100 / 10
		5. n的各位 = n % 10
		6. 判断即可
		 */
		int n = 154;
		int n1 = n / 100;
		int n2 = n % 100 / 10;
		int n3 = n % 10;
		if(n1 * n1 * n1 + n2 * n2 * n2 + n3 * n3 * n3 == n) {
			System.out.println(n + "是水仙花数");
		} else {
			System.out.println(n + "不是水仙花数");
		}
	}
}
  • 5
    没有输出
  • 6

public class Homework06 { 

	//编写一个main方法
	public static void main(String[] args) {
		/*
		输出1-100之间的不能被5整除的数,每5个一行

		思路分析
		1. 先输出1-100的所有数
		2. 然后过滤输出 不能被5整除的数 i % 5 !=0
		3. 每5个一行, 我们使用 int count 统计输出的个数 当 count%5=0就说明
			输出了5个,这时,我们输出 一个换行即可控制
		代码实现

		 */
		int count = 0; //统计输出的个数
		for(int i = 1; i <= 100; i++) {
			if(i % 5 != 0) {
				count++;
				System.out.print(i + "\t");

				//判断, 每满5个,就输出一个换行..
				if(count % 5 == 0) {
					System.out.println();
				}
			}
		}

	}
}
  • 7


public class Homework07 { 

	//编写一个main方法
	public static void main(String[] args) {

		//输出小写的a-z以及大写的Z-A
		//考察我们对 a-z编码和 for的综合使用
		//思路分析
		//1. 'b' = 'a' + 1 c = 'a' + 2
		//2. 使用for搞定
		
		for(char c1 = 'a'; c1 <= 'z'; c1++) {
			System.out.print(c1 +" ");
		} 
		System.out.println("============");
		//灵活的使用,编程..
		for(char c1 = 'Z'; c1 >= 'A'; c1--) {
			System.out.print(c1 +" ");
		}	
	}
}
  • 8

public class Homework08 { 

	//编写一个main方法
	public static void main(String[] args) {
		/*
		求出1-1/2+1/3-1/4…..1/100的和
		思路分析
		1. 1-1/2+1/3-1/4…..1/100 = (1/1)-(1/2)+(1/3)-(1/4)...1/100
		2. 从上面的分析我们可以看到 
		(1) 一共有100数 , 分子为1 , 分母从1-100
		(2) 还发现 当分母为奇数时,前面是 +, 当分母是偶数时,前面是-
		3. 我们可以使用 for + 判断即可完成
		4. 把结果存放到 double sum 
		5. 这里有一个隐藏的陷阱,要把 公式分子 1 写出1.0 才能得到精确的小数
		 */
		
		double sum = 0;
		for(int i = 1; i <= 100; i++) {
			//判断是奇数还是偶数,然后做不同的处理
			if( i % 2 != 0) {//分母为奇数
				sum += 1.0/i;
			} else { //分母我偶数
				sum -= 1.0/i;
			}
		}
		System.out.println("sum=" + sum);
		
	}
}
  • 9

public class Homework09 { 

	//编写一个main方法
	public static void main(String[] args) {
		//求(1)+(1+2)+(1+2+3)+(1+2+3+4)+...+(1+2+3+..+100)的结果
		//
		//思路分析
		//1. 一共有100项相加
		//2. 每一项的数字在逐渐增加
		//3. 很像一个双层循环
		//i 可以表示是第几项,同时也是当前项的最后一个数
		//4. 使用 sum 进行累计即可
		int sum = 0;
		for(int i = 1; i <= 100; i++) {
			for(int j = 1;j <= i; j++) {//内层对1-i进行循环
				sum += j;
			}
		}
		System.out.println("sum=" + sum);  
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值