第五章程序控制结构作业(韩顺平)

某人有100000元,每经过一个路口需要交费,现金>50000每次交5%,现金<=50000,每次交1000,计算可以经过多少次路口,用while-break
public class homework01 {
    public static void main(String[] args){
        int num = 0; //经过路口的次数
        double money = 100000; //初始钱数
        while (true) {
            if (money > 50000) {
                money = money - money * 0.05;
                num += 1;
            } else if (money >= 1000) {
                money = money - 1000;
                num += 1;
            } else {
                break;
            }
        }
        System.out.println("可以经过的路口数:"+num);
        System.out.println("剩余的钱数"+money);
    }
}

实现判断一个整数属于哪个范围(大于0,小于0,等于0)
import java.util.Scanner;
public class homework02 {
    public static void main(String[] args){
        Scanner myscanner = new Scanner(System.in);
        System.out.println("请输入一个整数:");
        int num = myscanner.nextInt();
        if (num>0){
            System.out.println(num+"大于0");
        }
        else if (num == 0){
            System.out.println(num+"等于0");
        }
        else {
            System.out.println(num+"小于0");
        }
    }
}

判断一个年份是否为闰年
import java.util.Scanner;

public class homework03 {
    public static void main(String[] args){
        Scanner myscanner = new Scanner(System.in);
        System.out.println("请输入一个年份:");
        int year = myscanner.nextInt();
        if ((year%4 == 0 && year%100 != 0) || year%400 == 0){
            System.out.println(year+"年是闰年");
        }
        else {
            System.out.println(year+"年不是闰年");
        }
    }
}

判断水仙花数

import java.util.Scanner;

public class homework04 {
    public static void main(String[] args){
        Scanner myscanner = new Scanner(System.in);
        System.out.println("请输入一个三位数整数:");
        int number = myscanner.nextInt();
        if (number < 100 || number >999){
            System.out.println("输入错误,请重新输入:");
            number = myscanner.nextInt();
        }
        int hundred_place = number/100;
        int ten_place = number%100/10;
        int one_place = number%10;
        if (number == (one_place*one_place*one_place + ten_place*ten_place*ten_place + hundred_place*hundred_place*hundred_place)){
            System.out.println(number+"是水仙花数");
        }
        else {
            System.out.println(number+"不是水仙花数");
        }
    }
}

代码输出

输出1-100之间不能被5整除的数,每5个一行
public class homework06 {
    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){
                System.out.print('\n');
                count = 0;
            }
        }
    }
}

输出小写的a-z和大写Z-A
public class homework07 {
    public static void main(String[] args){
        for (char ch1 = 'a'; ch1 <= 'z'; ch1++){
            System.out.print(ch1+"\t");
        }
        System.out.print("\n");
        for (char ch2 = 'Z'; ch2 >= 'A'; ch2--){
            System.out.print(ch2+"\t");
        }
    }
}

求1-1/2+1/3-1/4.......1/100的和

一共100个数,只有分母是1-100,分子不变,分母为奇数+,偶数-

public class homework08 {
    public static void main(String[] args){
        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);
    }
}

 陷阱:分子写成1.0才能精确到小数,不然输出1.0

 求1+(1+2)+(1+2+3)+.............+(1+2+3+......+100)的结果

100项想加,每一项数字逐渐增加,双层循环

public class homework09 {
    public static void main(String[] args){
        long sum = 0;
        for (int i = 1; i <= 100; i++){
            for (int j = 1; j <= i; j++){
                sum +=j;
            }
        }
        System.out.println(sum);
    }
}

171700 

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值