01-JAVA基础—>流程控制—>作业

本文展示了几个Java编程实例,包括根据积分计算购物折扣、确定年月的天数、计算一年中某日是当年的第几天,以及图形打印任务。此外,还涵盖了九九乘法表的汉字版和水仙花数的打印。代码涵盖条件判断、循环、输入输出及数学逻辑,是Java初学者的实用练习。
摘要由CSDN通过智能技术生成

早起的鸟儿有虫吃,瞎写的代码有Bug!

1、计算应缴金额

商场根据会员积分打折:
					 2000 分以内打 9 折, 
					 4000 分以内打 8 折, 
					 8000 分以内打 7.5 折,
					 8000 分以上打 7 折,
					 使用 if-else-if 结构,实现手动输入购物金额和积分, 计算出应缴金额
//1、计算应缴金额
	public static void x() {
		try (Scanner input = new Scanner(System.in)) {
			System.out.println("请输入您的积分数:"); 
			//在接收输入之前判断输入的类型是否为int
			if(input.hasNextInt()) {
				int score= input.nextInt();
				if(score >800){ 
					System.out.println("要打 7 折"); 
					}
				else if(score > 4000 & score <= 800){ 
					System.out.println("要打 7.5折"); 
					}
				else if(score > 2000 & score <= 4000){ 
					System.out.println("要打 8 折"); 
					}
				else{
					System.out.println("要打 9 折");
				}
			}else {
				System.out.println("你输入的不是积分数");
			}
		}
	}

2、计算该年该月总天数

题目:		一年中有 12 个月,而每个月的天数是不一样的。其中大月 31 天,
			分别为 1,3,5,7,8,10,12 月,小月 30 天,分别 为 4,6,9,11 月。
			还有二月比较特殊,平 年的二月只有 28 天,而闰年的二月有 29 天,
			由用户在控制台输入年份和月份, 程序计算该年该月的天数。

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

//2、计算该年该月天数
	public static void y1() {
		try (Scanner input = new Scanner(System.in)) {
			System.out.println("输入年份:");
			//在接收输入之前判断输入的类型是否为int
			if(input.hasNextInt()) {
				int year = input.nextInt();
				System.out.println("请输入月份:");
				int month= input.nextInt();
				boolean isRen;
				 if(year%4==0&&year%100!=0||year%400==0){
			         System.out.println(year+"是闰年");
			         isRen=true;
			     }else{
			         System.out.println(year+"是平年");
			         isRen=false;
			      }
				
				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;
				default:
					if(isRen) {day=29;}
					else{day=28;}
					break;
				}
				System.out.println(year+"年"+month+"月共有"+day+"天");	
			
			}else {
				System.out.println("你输入有错误");
			}
		}

2、输入某年某月某日,判断这一天是这一年的第几天? 多想了一下

在这里插入图片描述

//输入某年某月某日,判断这一天是这一年的第几天?  多想了一下
		public static void y2() {
		int year;
        int mouth;
        int day=0;
        int days;
        //累计天数
        int d=0;
        int e = 0;
        Scanner scanner = new Scanner(System.in);
        do {
            System.out.println("输入年:");
            year = scanner.nextInt();
            System.out.println("输入月:");
            mouth = scanner.nextInt();
            System.out.println("输入日:");
            days = scanner.nextInt();
            if (mouth < 0 || mouth > 12 || days < 0 || days > 31) {
                System.out.println("input error!");
                e = 1;
            }
        } while (e == 1);
        for (int i = 1; i <mouth; i++) {
            switch (i) {
            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: {
                /**
                 * 闰年:①:非整百年数除以4,无余为闰,有余为平;②整百年数除以400,无余为闰有余平
                 * 二月:平年28天、闰年29天
                 */
                if ((year % 100 !=0 &&year % 4 == 0) || (year % 100 == 0 && year%400==0)) {
                    day = 29;
                } else {
                    day = 28;
                }
            }
            default:
                break;
            }
            d+=day;
        }
        System.out.println("这是"+year+"年的第"+(d+days)+"天");
	}

3、图形打印任务

		//3、图形打印任务
		public static void z() {	
       //------------------------------------------------------------------------		
       //Demo1
//			*
//			**
//			***
//			****
//			*****
        for(int i=0;i<5;i++) {
			for(int j=0;j<i+1;j++) {
				System.out.print("*");
			}
			System.out.println();
		}
        System.out.println();
       //------------------------------------------------------------------------		
       //Demo2
//        *****
//        ****
//        ***
//        **
//        *
        for(int i=6;i>0;i--) {
			for(int j=0;j<i-1;j++) {
				System.out.print("*");
			}
			System.out.println();
		}
       //------------------------------------------------------------------------		
       //Demo3
		//------------------------------------------------------------------------		
//	    *
//	   ***
//	  *****
//	 *******
//	  *****
//	   ***
//	    *
	for(int i=0;i<4;i++) {
		for(int j=i+1;j<4;j++) {
			System.out.print(" ");
		}
		
		for(int j=0;j<2*i+1;j++) {
			System.out.print("*");
		}
		System.out.println();
	}
	for(int i=3;i>0;i--) {
		for(int j=i;j<4;j++) {
			System.out.print(" ");
		}
		
		for(int j=0;j<2*i-1;j++) {
			System.out.print("*");
		}
		System.out.println();
	}
	
	
	//------------------------------------------------------------------------		
	//空心
//      *     
//     * *    
//    *   *   
//   *     *  
//  *       * 
// *         *
//  *       * 
//   *     *  
//    *   *   
//     * *    
//      *     
	for(int i=6;i>=1;i--){   //菱形的上半部分  
        for(int j=1;j<=11;j++){  
            if(i==j||j==12-i){  
                System.out.print("*");  
            }else{  
                System.out.print(" ");  
            }  
        }  
        System.out.println();  
    }  
      
    for(int i=2;i<=6;i++){    //菱形的下半部分  
        for(int j=1;j<=11;j++){  
            if(i==j||j==12-i){  
                System.out.print("*");  
            }else{  
                System.out.print(" ");  
            }  
        }  
        System.out.println();  
    }
        
		
	}

4、打印九九乘法表 + 打印汉字版的九九乘法表

在这里插入图片描述

public static void k() {
		for (int i = 1; i <= 9; i++) {
			for (int j = 1; j <= i; j++) {
				System.out.print(i+"*"+j+"="+j*i+"	");
			}
			System.out.print("\n");
		}

		for (int num_1 = 1; num_1 <= 9; num_1++) {
			for (int num_2 = 1; num_2 <= num_1; num_2++) {
				System.out.print(convert(num_2) + convert((int) num_1));
				if (num_1 * num_2 < 10) {
					System.out.print("得");
					System.out.print(result(num_1 * num_2) + "\t");
				} else {
					System.out.print(result(num_1 * num_2) + "\t");
				}
 
			}
			System.out.println("\n");
		}
	}
		public static String result(int j) {// 定义多位转换
			String jj = "";
			int bit = 1;// 定义位数
			for (int i = 1; j >= Math.pow(10, i); i++) {// 判断输入数字位数
	 
				bit++;
			}
			String text = j + "";
			for (int bit_1 = 0; bit_1 < bit; bit_1++) {
				if (bit_1 >= 1) { // 判断是否需要加位
					jj = jj + addition(bit_1);
				}
				j = Integer.parseInt(text.substring(bit_1, bit_1 + 1));
				jj = jj + convert(j);
			}
			return jj;
		}
	 
		private static String addition(int bit_1) { // 定义加位
			String ii = "";
			switch (bit_1) {
			case 0:
				ii = "十";
				break;
			case 1:
				ii = "十";
				break;
			case 2:
				ii = "百";
				break;
			case 3:
				ii = "千";
				break;
			case 4:
				ii = "万";
				break;
			case 5:
				ii = "十万";
				break;
			case 6:
				ii = "百万";
				break;
			case 7:
				ii = "千万";
				break;
			case 8:
				ii = "亿";
				break;
			default:
				ii = "出错";
			}
			return ii;
		}

		public static String convert(int i) { // 定义单位转换
			String ii = "";
			switch (i) {
			case 0:
	 
				break;
			case 1:
				ii = "一";
				break;
			case 2:
				ii = "二";
				break;
			case 3:
				ii = "三";
				break;
			case 4:
				ii = "四";
				break;
			case 5:
				ii = "五";
				break;
			case 6:
				ii = "六";
				break;
			case 7:
				ii = "七";
				break;
			case 8:
				ii = "八";
				break;
			case 9:
				ii = "九";
				break;
			default:
				ii = "出错";
			}
			return ii;
		}

5、打印三位数中的所有水仙花数

水仙花数为:153
水仙花数为:370
水仙花数为:371
水仙花数为:407
public static void xx() { 
		for (int i = 100; i <1000 ; i++) {
			int firstNum = i/100;
			int secondNum = i/10%10;
			int thirdNum = i%10;
			if(firstNum*firstNum*firstNum + secondNum*secondNum*secondNum+thirdNum*thirdNum*thirdNum == i){
				System.out.println("水仙花数为:" + i);
			}
		}

	}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

高高飞起的勇敢麦当

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值