流程控制的逻辑训练


前言

本次有五个逻辑任务。


一、计算应缴金额



<font color=#999AAA >代码如下(示例):

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
			System.out.println("请输入您的积分");
		int integral = 0;
			integral = input.nextInt();
			if(integral<2000) {
				System.out.println("折扣为:" +0.9);
			}
			if(integral>=2000 && integral<4000) {
				System.out.println("折扣为:" +0.8);
			}
			if(integral>=4000 && integral<8000) {
				System.out.println("折扣为:" +0.75);
			}
			if(integral>=8000) {
				System.out.println("折扣为:" +0.7);
			}
	}

结果在这里插入图片描述
本次就使用了if-else if语句来做


二、计算该年该月天数

public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
			System.out.println("请输入年份:");
		Scanner onput = new Scanner(System.in);
			System.out.println("请输入月份:");
		int month=0 ;
		int year=0;
		year = onput.nextInt();
		month = input.nextInt();
		
		switch(month) {
		case 1 :
		case 3 :
		case 5 :
		case 7 :
		case 8 :
		case 10 :
		case 12 :
			System.out.println(31);break;
		case 4 :
		case 6 :
		case 9 :
		case 11 :
			System.out.println(30);break;
			/*
				判断是否为闰年,闰年的话应该是每四年就是一个闰年,
				然后在是100的倍数中不能被400整除的就不是闰年了
			*/
		case 2 :if(year%4 == 0) {
					if(year%400==100) {
						System.out.println(28);
					}
					else if(year%400==200) {
						System.out.println(28);
					}
					else if(year%400==300) {
						System.out.println(28);
					}
				else {
						System.out.println(29);
					}
				}else {
					System.out.println(29);
				};break;
		default : System.out.println("请输入正确的月份");
			}
	}

判断是否为闰年,并统计其二月的天数。


三、打印乘法表

public static void main(String[] args) {
		//判断有多少行
		for(int i=1; i<10; i++) {
			//判断每行有多少个
			for(int j=1; j<i+1; j++) {
				//按照乘法表的格式打印
				System.out.print(""+i+"*"+j+"="+i*j+" ");				
			}
			System.out.println();
		}
	}

结果在这里插入图片描述


四、打印水仙花数

public static void main(String[] args) {
		
		int x=100;
		//水仙花数应该小于1000
		while(x<999) {	
			//根据水仙花数的性质去判断
			x++;			
			int a=x/100;
			int b=x%100/10;
			int c=x%10;
			int e=a*a*a+b*b*b+c*c*c;
			if(x==e) {
				System.out.println("水仙花数="+e);
			}
		}
	

结果在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值