Java学习第二天

Java学习第二天

习题
1.用编程实现一下功能 某人有现金100000元,需要交费,规则如下:

1).某人有100,000元,每经过一次路口,需要交费,规则如下:1)当现金>50000时,每次交5%
2)当现金<=50000时,每次交1000
编程计算此人可以经过多少路口 要求while break完成

public class Main{
    public static void main(String []args) {
         double money = 100000;
        int count = 0;
        while (money >= 0){
            if (money > 50000){
                count++;
                money = money*0.95;
            }
            else if (money <= 50000){
                money-=1000;
                count++;
            }
            else {
                break;
            }
        }
        System.out.println(count);
    }
}
2.实现判断一个整数,属于哪个范围:大于0;小于0;等于0
public class Main{
    public static void main(String []args) {
    		Scanner sc = new Scanner(System.in);//定义一个Scanner对象
        int number = sc.nextInt();//输入一个整数
        if(number > 0){
            System.out.println("大于0");
        }
        else if (number < 0){
            System.out.println("小于0");
        }
        else  System.out.println("等于0");
    }
}
3.判断一个年份是否为闰年

`public class Main{
public static void main(String []args) {

int year;
Scanner sc = new Scanner(System.in);
System.out.println("请输入年份");
year = sc.nextInt();
if (year % 4 == 0){
    System.out.println( year + "年是闰年");
}
else {
    System.out.println( year + "年不是闰年");
}
    }

}```

###### 4.判断一个整数是否是水仙花数,所谓水仙花数是指一个3位数,其各个位上数字立方和等于其本身。例如:153 =1*1*1+3*3*3+5*5*5

```java
public class Main{
    public static void main(String []args) {
			Scanner sc = new Scanner(System.in);//定义一个Scanner对象
        System.out.println( "请输入一个三位数:");
        int number = sc.nextInt();//输入一个整数
        int a = number % 10;//或得个位数
        int b = (number / 10)%10;//或得十位数
        int c=(number/100)%10;//或得百位数
        if (number == (a * a *a) + (b * b * b) + (c * c * c)){
            System.out.println(number + "是水仙花数");
        }
        else {
            System.out.println( number + "不是水仙花数");
        }
    }
}
5.输出1-100之间的不能被5整除的数,每5个一行
public class Main{
    public static void main(String []args) {

int i = 1;
int count = 0;//记录不能被整除个数是否到达5个
while (i < 101){
    if ( i % 5 != 0){
        System.out.print( i + " ");
        count++;
    }
    if (count == 5){
        count = 0;//重新记录count
        System.out.println();//换行
    }
    i++;
}

    }

}

6.输出小写的a-z以及大写的Z-A

public class Main{
    public static void main(String []args) {

char a = 'a';
int i = 0;
while (i < 26){
    System.out.print((char)(a + i) + " ");
    i++;
}
System.out.println();
char b = 'A';
i = 0;
while (i < 26){
    System.out.print((char)(b + i) + " ");
    i++;
}
}

}
7.求出1-1/2+1/3-1/4…1/100的和
public class Main{
    public static void main(String []args) {
double sum = 0;
double i = 1.0;
while (i < 101){
    sum += ((1 / i) - ( 1 / (i + 1.0)));
    i++;
}
System.out.println(sum);
    }

}
8.求1+(1+2)+(1+2+3) +(1+2+3+4) +…+(1+2+3+.+100)的结果
public class Main{
    public static void main(String []args) {
int sum = 0;
for (int i = 1; i < 101; i++){
    for (int j = 1; j <= i; j++){
        sum += j;
    }
}
System.out.println(sum);
    }

}

总结

今天就只是做了几道题,然后就开始打游戏,打完游戏又开始后知后觉,一天就这样迷迷糊糊的过去了,明天开始每天至少学习6个小时,4月20日完成java整套内容学习,然后开始学习Java框架。此贴为证!!!
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值