Java基础-day02-练习题

break语句细节(continune不写了,类似)

作业time到!

public class Hw1{
    public static void main(String[] args){
        double money = 100000;
        int count = 0;
        while( money >= 1000){ //很重要!!!别写成>0了!!!
            if (money > 50000){
                money = money * 0.95;
                count++;
                continue;
            }
            if ( money <= 50000){
                money -= 1000;
                count++;
                continue;
            }
        }
        System.out.println("count=" + count);
    }
}

一开始没考虑完整,写的while money>0,算成63次了,但只要这个人少于1000块钱,就无法通过路口,淦,还是鲁莽了。然后就是,我这种写法吧,额咋说呢,就是感觉没有老师给的那种思路好,他用的多分支,我这个,思路不够清晰,嗯嗯,用老师给的思路写一份:

public class Hw1{
    public static void main(String[] args){
        double money = 100000;
        int count = 0;
        while(true){
            if (money > 50000){
                money = money * 0.95;
                count++;
            }else if ( money <= 50000 && money > 1000){
                money -= 1000;
                count++;
            }else{
                break;
            }
        }
        System.out.println("count=" + count);
    }
}

第二题:

import java.util.Scanner;
public class Hw1{
    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 Hw1{
    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 Hw1{
    public static void main(String[] args){
        Scanner myScanner = new Scanner(System.in);
        System.out.println("请输入您要判断的数字");
        int num = myScanner.nextInt();
        int ge = num % 10;
        int shi = (num /10 ) % 10;
        int bai = num/100;
        System.out.println("ge = " + ge + "shi = " + shi + "bai = " + bai );
        if (ge*ge*ge + shi*shi*shi + bai*bai*bai == num){
            System.out.println(num + "是水仙花数");
        }else{
            System.out.println(num + "不是水仙花数");
        }
    }
}

第四题:

import java.util.Scanner;
public class Hw1{
    public static void main(String[] args){
        int count = 0;
        for (int i = 1; i <= 100; i++){
            if ( i % 5 != 0 ){  //注意两个if的嵌套
                count++;
                System.out.print(i+" ");
                if(count % 5 == 0){ // 注意用模5来做
                    System.out.println("");
                    count = 0;
                }
            } 
        }
    }
}

第五题:

// public class Hw1{
// //自己写的垃圾方法
//     public static void main(String[] args){
//         char zimu = 'A';
//         char xiaoxie = 'a';
//         for(int i = 0; i <= 25; i++){
//             char daxie  = (char)(zimu + i);
//             System.out.print(daxie +" ");
//         }
//         System.out.println("");
//         for(int i = 0; i <= 25; i++){
//             char xiaoxie_2 = (char)(xiaoxie + i);
//             System.out.print(xiaoxie_2 +" ");
//         }
//     }
// }
//还是老师的方法比较好
public class Hw1{
    public static void main(String[] args){
        for (char c1 = 'a'; c1 <= 'z'; c1++){
            System.out.print(c1);
        }
        System.out.println("");
        for (char c2 = 'A'; c2 <= 'Z'; c2++){
            System.out.print(c2);
        }
    }
}

第六题:

public class Hw1{
//自己写的垃圾方法
    public static void main(String[] args){
        double sum = 0;
        double ans = 0;
        for(int i = 1; i <= 100; i++){
            if (i % 2 == 1){ // 判断奇数偶数 
                 ans = 1.0/i; // 隐藏陷阱:要写1.0不然当成你整数除法,很多东西算出来都是0
            }else if(i % 2 == 0){
                 ans = -1.0/i;
            }
            sum += ans;
        }
        System.out.println("结果是:" + sum);
    }
}

第七题:

public class Hw1{
    public static void main(String[] args){
        int sum = 0;
        for (int i = 1; i <= 100; i++){
            for (int j = 1; j <= i; j++){
                sum += j;
            }
        }
        System.out.println("sum = " + sum);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值