【Java循环练习 - 韩顺平】


1. 镂空金字塔

public class Test04 {
	public static void main(String[] args) {
		/*
			思路:
			1. 打印矩形
			2. 打印一半矩形
			3. 打印金字塔
			4. 镂空
		*/
		// 设置层数
		int level = 10;
		for ( int i = 0; i < level; i++ ) {
			// 控制每层打印空格的数量 0-4 1-3 2-2 3-1 4-0	
			for ( int j = 0; j < level - i - 1; j++ ) {
				System.out.print(" ");
			}
			// 控制每层打印*的数量
			for ( int j = 0; j < 2 * i + 1; j++ ) {
				// 如果是最后一行,直接打印*号
				if ( i == level - 1 ) {
					System.out.print("*");
				} else if ( j == 0 || j == 2 * i ) {
					// 控制每层只打印第一个和最后一个*
					System.out.print("*");
				} else {
					// 其余的情况打印空格
					System.out.print(" ");
				}
				//System.out.print("*");
			}
			//System.out.println("*");
			// 换行
			System.out.println("");
		}
	}
}

2. 某人有10万元,每经过一次路口,需要缴费,规则如下:
   当现金大于5万元时,每次交5%;当现金小于等于5万元时,每次交1000
   计算可以经过多少次路口,使用while-break方式完成

public class Test05 {
	public static void main(String[] args) {
		// 经过路口的次数统计
		int count = 0;
		// 总金额
		double total = 100000;
		while ( true ) {
			if ( total < 1000 ) {
				break;
			} else if ( total > 50000 ) {
				total -= total * 0.05;
			} else {
				total -= 1000;
			}
			count++;
		}
		System.out.println("一共可以经过路口次数 = " + count);
	}
}

3. 实现判断一个整数,属于哪个范围:大于0,小于0,等于0

import java.util.Scanner;

public class Test06 {
	public static void main(String[] args) {
		// 从控制台读入一个整型数据
		Scanner scanner = new Scanner(System.in);
		int data = scanner.nextInt();
		if ( data == 0 ) {
			System.out.println("该整数等于零");
		} else if ( data > 0 ) {
			System.out.println("该整数大于零");
		} else {
			System.out.println("该整数小于零");
		}
	}
}

4. 判断一个年份是否为闰年

import java.util.Scanner;

public class Test07 {
	public static void main(String[] args) {
		/*
			思路:
			1. 能被4整除但是不能被100整除的年份
			2. 能被400整除的年份
		*/
		Scanner scanner = new Scanner(System.in);
		int year = scanner.nextInt();
		if ( (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ) {
			System.out.println("该年是闰年");
		} else {
			System.out.println("该年不是闰年");
		}
	}
}

5. 判断一个整数是否是水仙花数
   水仙花数是指一个3位数,其各位上数字立方和等于其本身

import java.util.Scanner;
import java.lang.Math;

public class Test08 {
	public static void main(String[] args) {
		// 从控制台读入一个整数
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		int temp = num;
		// 记录该整数各位的立方和
		int sum = 0;
		while ( num / 10 != 0 ) {
			sum += Math.pow(num % 10, 3);
			num /= 10;
		}
		sum += Math.pow(num % 10, 3);
		if ( sum == temp ) {
			System.out.println("该整数是水仙花数");
		} else {
			System.out.println("该整数不是水仙花数");
		}
	}
}

6. 输出1-100之间的不能被5整除的数,每五个一行

public class Test09 {
	public static void main(String[] args) {
		int count = 0;
		for ( int i = 1; i <= 100; i++ ) {
			if ( i % 5 != 0 ) {
				count++;
				System.out.print(i + "\t");
				if ( count % 5 == 0 ) {
					System.out.println();
				}
			}
		}
	}
}

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

public class Test10 {
	public static void main(String[] args) {
		/*
			思路:
			1. 小写的a-z 97-122
			2. 大写的Z-A 90-65
		*/
		/*
		for ( int i = 97; i <= 122; i++ ) {
			System.out.print( (char)i );
		}
		System.out.println();
		for ( int i = 90; i >=65; i-- ) {
			System.out.print( (char)i );
		}
		*/
		for ( char i = 'a'; i <= 'z'; i++ ) {
			System.out.print(i);
		}
		System.out.println();
		for ( char i = 'Z'; i >= 'A'; i-- ) {
			System.out.print(i);
		}
	}
}

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

public class Test11 {
	public static void main(String[] args) {
		double result = 0;
		for ( int i = 1; i <= 100; i++ ) {
			if ( i % 2 == 0 ) {
				result += -(double)1 / i;
			} else {
				result += (double)1 / i;
			}
		}
		System.out.println("最后的结果 = " + result);
	}
}

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值