java从入门到入土第5天

Day05

import java.util.Scanner;
public class Work01{
	/**
		1.某朋友说,
			如果存款超过100万,则入手宝马X5,
			否则超过50万,则入手奥迪A6,
			否则超过20万,则入手比亚迪,
			否则超过10万则玩玩极品飞车游戏,
			请为他编写一个购车选择程序,根据输入的金额,提示可以购买的车型。
	*/
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入金额:");
		double money = scan.nextDouble();
		
		if(money > 1000000){
			System.out.println("入手宝马X5");
			
		}else if(money > 500000){
			System.out.println("入手奥迪A6");
			
		}else if(money > 200000){
			System.out.println("入手比亚迪");
			
		}else if(money > 100000){
			System.out.println("玩玩极品飞车游戏");
			
		}else{
			System.out.println("好好学习Java~~~");
			
		}
		
		
	}
}
import java.util.Scanner;
public class Work02{
	/**
		2.设计一个考试奖励神器,根据录入学员的分数x给予奖励,
		如果分数x小于等于80分的则不予奖励并罚一个iphone6s,
		如果分数80<x<=90则奖励一个iphone4,
		如果分数90<x<=100则奖励一个iphone4,再奖励一个ipad
	*/
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入成绩:");
		double x = scan.nextDouble();
		
		if(x <= 80){
			System.out.println("不予奖励并罚一个iphone6s");
			
		}else if(x > 80 && x<=90){
			System.out.println("奖励一个iphone4");
			
		}else if(x > 90 && x<=100){
			System.out.println("奖励一个iphone4,再奖励一个ipad");
			
		}
		
	}
}
import java.util.Scanner;
public class Work03{
	/**
		3.从键盘录入输入3 个数a,b,c,按从大到小进行输出
		
		经验:简单的if分支使用三目运算符代替
	*/
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入第一个数字:");
		int a = scan.nextInt();
		System.out.println("请输入第二个数字:");
		int b = scan.nextInt();
		System.out.println("请输入第三个数字:");
		int c = scan.nextInt();
	
		//最大值
		int max = a;
		if(max < b){
			max = b;
		}
		if(max < c){
			max = c;
		}
		
		//最小值
		int min = a;
		if(min > b){
			min = b;
		}
		if(min > c){
			min = c;
		}
		
		//中间值
		int mid = a+b+c-max-min;
		
		System.out.println(max + ">" + mid + ">" + min);
	}
}
import java.util.Scanner;
public class Work04{
	/**
		4.计算个人所得税,关乎大家的生计
			要求使用程序实现个人所得税计算器,
			实现从键盘录入当月工资收入,输出应缴个人所得税。
			
		工资个税的计算公式为:
			应纳税额 = (工资薪金所得 - 扣除数)* 适用税率-速算扣除数;

	*/
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入当月工资:");
		double salary = scan.nextDouble();
		
		double deductionAmount = 3500;//扣除数
		double taxSalry = salary-deductionAmount;//应该上税工资部分
		double taxRate = 0;//税率
		double quickCalculationDeduction = 0;//速算扣除数
		
		if(taxSalry > 0){
			
			if(taxSalry < 1500){
				taxRate = 0.03;
				quickCalculationDeduction = 0;
				
			}else if(taxSalry < 4500){
				taxRate = 0.1;
				quickCalculationDeduction = 105;
				
			}else if(taxSalry < 9000){
				taxRate = 0.2;
				quickCalculationDeduction = 555;
				
			}else if(taxSalry < 35000){
				taxRate = 0.25;
				quickCalculationDeduction = 1005;
				
			}else if(taxSalry < 55000){
				taxRate = 0.3;
				quickCalculationDeduction = 2755;
				
			}else if(taxSalry < 80000){
				taxRate = 0.35;
				quickCalculationDeduction = 5505;
				
			}else{
				taxRate = 0.45;
				quickCalculationDeduction = 13505;
				
			}
			
			double tax = taxSalry * taxRate - quickCalculationDeduction;
			System.out.println("当月应上税:" + tax);
		}else{
			System.out.println("好好努力,你现在的工资不用上税");
		}
		
		
	}
}
import java.util.Scanner;
public class Work05{
	/**
		5.张三为他的手机设定了自动拨号(switch)
			按1:拨爸爸的号
			按2:拨妈妈的号
			按3:拨爷爷的号
			按4:拨奶奶的号
	*/
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入快速拨号:");
		int num = scan.nextInt();
		
		switch(num){
			case 1:
				System.out.println("拨爸爸的号");
			break;
			case 2:
				System.out.println("拨妈妈的号");
			break;
			case 3:
				System.out.println("拨爷爷的号");
			break;
			case 4:
				System.out.println("拨奶奶的号");
			break;
			default:
				System.out.println("拨号错误");
			break;
		}
		
	}
}
public class Test01{
	/**	
		知识点:for循环嵌套
		
		需求1:打印以下图形
			**** i=0 (j=0,1,2,3)
			**** i=1 (j=0,1,2,3)
			**** i=2 (j=0,1,2,3)
			for(int i = 0;i<3;i++){
				for(int j = 0;j<4;j++){
					System.out.print("*");
				}
				System.out.println();
			}
			
		需求2:打印以下图形
			*		i=0 (j=0)
			**		i=1 (j=0,1)
			***		i=2 (j=0,1,2)
			****	i=3 (j=0,1,2,3)
			*****	i=4 (j=0,1,2,3,4)
			for(int i = 0;i<5;i++){
				for(int j = 0;j<=i;j++){
					System.out.print("*");
				}
				System.out.println();
			}
		
		需求3:打印以下图形
			    *
			   **
			  ***
			 ****
			*****
			for(int i = 0;i<5;i++){
				for(int k = 0;k<4-i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<=i;j++){
					System.out.print("*");
				}
				System.out.println();
			}
		
		需求4:打印以下图形
			*****
			****
			***
			**
			*
			for(int i = 0;i<5;i++){
				for(int j = 0;j<5-i;j++){
					System.out.print("*");
				}
				System.out.println();
			}
		
		需求5:打印以下图形
			*****
			 ****
			  ***
			   **
			    *
			for(int i = 0;i<5;i++){
				for(int k = 0;k<i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<5-i;j++){
					System.out.print("*");
				}
				System.out.println();
			}
				
		需求6:打印以下图形
			   *      i = 0
			  ***     i = 1
			 *****    i = 2
			*******   i = 3
			for(int i = 0;i<4;i++){
				for(int k = 0;k<3-i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<i*2+1;j++){
					System.out.print("*");
				}
				System.out.println();
			}
		
		需求7:打印以下图形
			   *
			  * *
			 *   *
			*******
			for(int i = 0;i<4;i++){
				for(int k = 0;k<3-i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<i*2+1;j++){
					if(i==0 || i==3 || j==0 || j== i*2){//第一行、最后一行、每行第一列、每行最后一列
						System.out.print("*");
					}else{
						System.out.print(" ");
					}
				}
				System.out.println();
			}
		
		需求8:打印以下图形
			*******
			 *****
			  ***
			   *
			for(int i = 0;i<4;i++){
				for(int k = 0;k<i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<7-i*2;j++){
					System.out.print("*");
				}
				System.out.println();
			}
		
		需求9:打印以下图形
			*******
			 *   *
			  * *
			   *
			for(int i = 0;i<4;i++){
				for(int k = 0;k<i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<7-i*2;j++){
					if(i==0 || i==3 || j==0 || j== 7-i*2-1){//第一行、最后一行、每行第一列、每行最后一列
						System.out.print("*");
					}else{
						System.out.print(" ");
					}
				}
				System.out.println();
			}   
			
		需求10:九九乘法表
			解决方案一:
				for(int i = 1;i<=9;i++){	
					for(int j = 1;j<=i;j++){
						System.out.print(j + "x" + i + "=" + (i*j) + "\t");
					}
					System.out.println();
				}
		
			解决方案二:
				for(int i = 1;i<=9;i++){
					for(int k = 1;k<i;k++){
						System.out.print("\t");
					}
					for(int j = i;j<=9;j++){
						System.out.print(i + "x" + j + "=" + (i*j) + "\t");
					}
					System.out.println();
				}
	*/
	public static void main(String[] args){
		
		
		
	}
}
public class Test02{
	/**	
		知识点:while循环
		
		语法结构:
			while(表达式){
				...代码块...
			}
			
		理解:表达式的结果必须是boolean类型
			true -- 执行代码块
			false - 跳出整个循环
			
		需求:使用while循环打印5遍"用良心做教育"
			int i = 0;
			while(i<5){
				System.out.println("用良心做教育");
				i++;
			}
			
		死循环:
			while(true){
				System.out.println("死循环");
			}
			
		案例:我有个梦想,每月存3000,每年递增1000元,多少个月后存满20万
			int allMoney = 0;
			int money = 3000;
			int month = 0;
			
			while(allMoney < 200000){
				allMoney += money;
				month++;
				if(month % 12 == 0){
					money+=1000;
				}
			}
			
			System.out.println(month + "个月后存满20万");
			System.out.println(money);
			
		小结:while循环可以做到for循环的功能
	*/
	public static void main(String[] args){
		
		
	}
}
import java.util.Scanner;
public class Test03{
	/**	
		知识点:do-while循环
		
		语法结构:
			do{
				...代码块...
			}while(表达式);
			
		理解:
			先执行一遍代码块,再判断表达式
			表达式的结果必须是boolean类型
				true -- 执行代码块
				false - 跳出整个循环
			
		做实验:
			do{
				System.out.println("用良心做教育");
			}while(false);
			
		案例:唐亮参加学校组织的歌咏比赛,大赛在即,
			  老师建议:先彩排一次,如果很令人满意,以后就不用彩排了,
			  否则每天都排,直到现场表现满意为止!
			Scanner scan = new Scanner(System.in);
		
			String str;
			do{
				System.out.println("唐亮:\"旋转、跳跃,我闭着眼~~~\"");
				System.out.println("唐亮:\"何老师,您满意了吗?\"");
				str = scan.next();
				
			}while(str.equals("不满意"));
		
		小结:
			1.do-while先循环一遍,再判断
			2.在代码块中声明的变量不能在外面使用
			
		知识点:for vs while vs do-while
			表达式的区别:
				for(初始化变量;判断条件;更新变量){}
				while(判断条件){}
				do{}while(判断条件);
				注意:三大循环的判断条件,如果是true就执行代码块,如果是false就跳出循环
				
			执行顺序的区别:
				for:先判断,再执行
				while:先判断,再执行
				do-while:先执行一遍,再判断
				
			应用场景的区别:
				for:循环次数确定时
				while:循环次数不确定时,先判断,再执行
				do-while:循环次数不确定时,先执行一遍,再判断
		
	*/
	public static void main(String[] args){
		
		
		
	}
}
import java.util.Scanner;
public class Test04{
	/**	
		知识点:特殊的流程控制语句 -- break
		含义:作用于循环中,表示跳出/结束当前循环
		
		做实验:
			while(true){
				System.out.println("111");
				System.out.println("222");
				if(true){
					break;
				}
				System.out.println("333");
			}
			
		案例:循环录入麻生希同学5门课的成绩并计算平均分,
			  如果某分数录入为负,停止录入并提示。
	*/
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		
		boolean flag = true;//true-正常录入   false-非正常录入
		
		double sum = 0;
		for(int i = 1;i<=5;i++){
			System.out.println("请输入第" + i + "门成绩:");
			double score = scan.nextDouble();
			if(score < 0){
				flag = false;
				break;
			}
			sum += score;
		}
		
		if(flag){
			double avg = sum/5;
			System.out.println("平均分为:" + avg);
		}else{
			System.out.println("分数为负数,停止录入");
		}
		
		
		
	}
}
import java.util.Scanner;
public class Test05{
	/**	
		知识点:特殊的流程控制语句 -- continue
		含义:作用于循环中,表示跳过循环体剩余部分,进入到下一次循环中
		
		做实验:
			while(true){
				System.out.println("111");
				System.out.println("222");
				if(true){
					continue;
				}
				System.out.println("333");
			}
		
		案例:循环录入Java课5名学生的成绩,统计分数大于等于80分的学生比例。
		解决方案一:
			Scanner scan = new Scanner(System.in);
	
			int count = 0;//分数大于等于80分的学生个数
			
			for(int i = 1;i<=5;i++){
				System.out.println("请输入第" + i + "名学生的成绩:");
				double score = scan.nextDouble();
				
				if(score >= 80){
					count++;
				}
			}
			
			double proportion = count/5.0*100;
			System.out.println("成绩大于等于80分学生的比例为:" + proportion + "%");
			
		解决方案二:
			Scanner scan = new Scanner(System.in);
	
			int count = 0;//分数大于等于80分的学生个数
			
			for(int i = 1;i<=5;i++){
				System.out.println("请输入第" + i + "名学生的成绩:");
				double score = scan.nextDouble();
				if(score<80){
					continue;
				}
				count++;
			}
			
			double proportion = count/5.0*100;
			System.out.println("成绩大于等于80分学生的比例为:" + proportion + "%");
		
	*/
	public static void main(String[] args){
		
		
	}
}
public class Test06{
	/**	
		知识点:特殊的流程控制语句 -- return
		含义:作用于方法中,表示结束当前方法

	*/
	public static void main(String[] args){
		
		System.out.println("111");
		System.out.println("222");
		if(true){
			return;
		}
		System.out.println("333");
	}
}
public class Test07{
	/**	
		知识点:特殊的流程控制语句 -- label

	*/
	public static void main(String[] args){
		
		//面试题:以下代码是否会报错?
		//答案:不会
		https://www.baidu.com/
		for(int i = 1;i<=5;i++){
			System.out.println("用良心做教育");
		}
	}
}
public class Test08{
	/**	
		知识点:特殊的流程控制语句 -- label
		理解:给循环做标记
		
		需求:
			外层循环5次
			内层循环3次
			当外层循环到第2次时,在内层循环中关闭掉外层循环
	*/
	public static void main(String[] args){
		
		a:for(int i = 1;i<=5;i++){
			for(int j = 1;j<=3;j++){
				System.out.println(i + " -- " + j);
				if(i == 2){
					break a;//跳出指定循环
				}
			}
		}
	}
}
import java.util.Scanner;
public class Test09{
	/**	
		知识点:万年历
		
		需求:输入年和月,打印当月的日历
		线索:1900年1月1日是星期一
		
		要想打印当月的日历,需要哪些数据作为支持?
			年份、月份、当月天数、当月第一天是星期几
	*/
	public static void main(String[] args){
		
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入年:");
		int year = scan.nextInt();
		System.out.println("请输入月:");
		int month = scan.nextInt();
		
		//当月天数
		int day = 0;
		switch(month){
			case 1:case 3:case 5:case 7:case 8:case 10:case 12:
				day = 31;
			break;
			case 4:case 6:case 9:case 11:
				day = 30;
			break;
			case 2:
				if(year%4==0 && year%100!=0 || year%400==0){
					day = 29;
				}else{
					day = 28;
				}
			break;
		}
		
		//计算总天数 -- 1900.1.1 ~ 输入年输入月的第一天
		
		
		//计算年的总天数 -- 1900(包含)~输入年(排他)
		int allDayOfYear = 0;
		for(int i = 1900;i<year;i++){
			if(i%4==0 && i%100!=0 || i%400==0){
				allDayOfYear += 366;
			}else{
				allDayOfYear += 365;
			}
		}
		
		//计算月的总天数 -- 1~输入月
		int allDayOfMonth = 0;
		for(int i = 1;i<month;i++){
			switch(i){
				case 1:case 3:case 5:case 7:case 8:case 10:case 12:
					allDayOfMonth += 31;
				break;
				case 4:case 6:case 9:case 11:
					allDayOfMonth += 30;
				break;
				case 2:
					if(year%4==0 && year%100!=0 || year%400==0){
						allDayOfMonth += 29;
					}else{
						allDayOfMonth += 28;
					}
				break;
			}
		}
		
		//合并总天数
		int allDay = allDayOfYear + allDayOfMonth + 1;
		
		//计算星期
		int week = allDay%7;//0~6
		if(week == 0){
			week = 7;
		}
		
		//打印日历
		
		System.out.println(" --- " + year + "年" + month + "月 ---");
		System.out.println("一\t二\t三\t四\t五\t六\t日");
		
		int count = 0;
		
		for(int i = 1;i<week;i++){
			System.out.print("\t");
			count++;
		}
		
		for(int i = 1;i<=day;i++){
			System.out.print(i + "\t");
			count++;
			if(count % 7 == 0){
				System.out.println();
			}
		}
		
		
		
		
		
	}
}

1.嵌套for循环 – 重要
经验:慢慢捋思路

2.while循环

3.do-while循环

4.for vs while vs do-while
表达式的区别
执行顺序的区别
应用场景

5.特殊的流程控制语句
break
continue
return
label

6.万年历 – 重要

作业:
1.知识点的梳理文档
2.代码编写3遍
3.课后作业
4.复习:本周所有内容
5.预习:方法、数组、面向对象
6.提升作业:分别使用for循环、while循环实现
*

   * *
  *   *
   * *
    *
  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值