【Java学习】小练习

import java.util.Scanner;

// 一个Java源文件中可以有多个类,但是只能有一个public类
// 多个类中可以有多个 main 方法,一个类中只能有一个 main 方法
// 使用的时候,先编译Java源文件  javac HomeWork01.java ,
// 在通过  java 类名 (java HomeWork04)  的方法在命令行中运行
public class HomeWork01{
	public static void main(String[] args){
		double totalMoney = 100000.0;
		double money = totalMoney;
		int count = 0; // 统计可以经过路口的次数
		while(true){
			if(money > 50000){
				money -= money * 0.05;
			}else if(money > 0 && money <= 50000){
				money -= 1000;
			}else{
				break;
			}
			count++;
		}
		System.out.println(count);
	}
}


class HomeWork02{
	public static void main(String[] args){
		//判断某一年是否为闰年
		Scanner sc = new Scanner(System.in);
		while(true){
			System.out.println("请输入年份(输入-1退出判断):");
			int year = sc.nextInt();
			if(year == -1){
				break;
			}
			if( ( year % 400 == 0 ) || ( year % 100 != 0 && year % 4 == 0 )){
				System.out.println(year + "年是闰年");
			}else{
				System.out.println(year + "年不是闰年");
			}
		}
	}
}


class HomeWork03{
	public static void main(String[] args){
		//判断水仙花数
		Scanner sc = new Scanner(System.in);
		while(true){
			System.out.println("请输入一个三位数字(输入-1退出循环):");
			int isNarcissusNum = sc.nextInt();
			if(isNarcissusNum == -1){
				break;
			}
			int ge = isNarcissusNum % 100 % 10; // 获取个位上的数
			int shi = isNarcissusNum / 10 % 10; // 获取十位上的数
			int bai = isNarcissusNum / 100; 	//获取百位上的数字
			if( isNarcissusNum == ge * ge * ge + shi * shi * shi + bai * bai * bai){
				System.out.println(isNarcissusNum + "是水仙花数");
			}else{
				System.out.println(isNarcissusNum + "不是水仙花数");
			}
		}
	}
}


class HomeWork04{
	public static void main(String[] args){
		//判断1-100之间不能被5整除的数
		Scanner sc = new Scanner(System.in);
		int count = 0; // 记录不能被5整除的数的个数
		for(int i = 1; i <= 100 ; i++){
			if(i % 5 != 0){
				System.out.print(i + "  ");
				count++;
				if(count % 5 == 0){
					System.out.println("\n");
				}
			}
		}
	}
}



class HomeWork05{
	public static void main(String[] args){
		//输出小写的 a - z
		for(char a = 97 ; a <= 'z' ; a++){
			System.out.print(a + "  ");
		}
		//换行
		System.out.println("");
		//输出大写的 A - Z
		for(char a = 65 ; a <= 'Z' ; a++){
			System.out.print(a + "  ");
		}
	}
}


class HomeWork06{
	public static void main(String[] args){
		// 1 - 1 / 2 + 1 / 3 - 1 / 4 ... 1 / 100 的和
		double sum = 0.0;
		for(int i = 1 ; i <= 100 ; i++){
			sum += Math.pow(-1 , i - 1) * 1 / i;
		}
		System.out.println(sum);
	}
}



class HomeWork07{
	public static void main(String[] args){
		// 求 1 + ( 1 + 2) + ( 1 + 2 + 3) + ... + ( 1 + 2 + ... + 100) 的和
		int sum = 0;
		for(int i = 1 ; i <= 100 ; i++){
			for(int j = 1 ; j <= i ; j++){
				sum += j;
			}
		}
		System.out.println(sum);
	}
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值