JAVA范例 三)条件控制语句(2)

实例25  求1到100之间的和

public class WhileCycle_01 {
	public static void main(String[] args) {
		int i = 0, sum = 0;
		while (i < 100) {
			++i;
			sum = sum + i;
		}
		System.out.println("1到100的和为:" + sum);
	}
}

 

 实例26  存上100元需要多少天

public class WhileCycle_02 {
	public static void main(String[] args) {
		int sum = 100;					// 最终的目标
		double db = 2.5;				// 每次存放的钱数
		int day = 1;					// 天数
		double dsum = 0;				// 每次存放的总数
		while (true) {
			dsum = dsum + db;			// 返回每天都存放钱的总和
			if (day % 5 == 0) {			// 判断是不是5的倍数
				dsum = dsum - 6;		// 从总数中扣去6元
				System.out.println("第" + day + "天花去6元,还剩" + dsum + "元!");
			}
			boolean flag = dsum >= sum;	// 求dsum是否大于sum的boolean值
			if (flag) {					// 如果是true则输出最终的结果,并跳出循环
				System.out.println("要经过连续存储" + day + "天,才能存上100元!");
				break;
			} else {					// 否则天数加1,也就是继续存钱
				day++;
			}
		}
	}
}

 

实例27  输出100之间的所有偶数

public class WhileCycle_03 {
	public static void main(String[] args) {
		int rows = 0; 					// 用于控制记录偶数的个数
		System.out.println("1~100之间的偶数为:");
		int i=1;
		while(true){
			if(i>100){
				break;
				}else{
				i++;
					}
			if (i % 2 == 1) { 			// 是奇数就结束当前这轮循环
				continue;
			} else {
				rows++;
				System.out.print(i + "  ");
				if (rows % 5 == 0)
					System.out.println(); // 每5个偶数一换行
			}

			}
		}
	}

 

实例28  如何判断回文数字

import java.util.Scanner;

public class WhileCycle_05 {
	public static void main(String[] args) {
		int n;
		System.out.println("请输入一个整数:");
		while (true) {						// 直到输入的数字为回文,才跳出循环
			Scanner scByte = new Scanner(System.in);
			n = scByte.nextInt();
			if (isPalindrome(n)) {
				System.out.println(n + " 是回文!");
				break;
			} else {
				System.out.println(n + " 不是回文!!");
			}
		}
	}
	public static boolean isPalindrome(int n) { 	// 判断输入的数字是否是回文
		int m = reverse(n);
		if (m == n)
			return true;
		else
			return false;
	}
	public static int reverse(int i) {				// 将输入的数字进行倒置
		int s, j = 0;
		s = i;
		while (s != 0) {
			j = j * 10 + s % 10;
			s = s / 10;
		}
		return j;
	}
}

 

实例29  输出100之间的所有奇数

public class DoWhileCycle_01 {
	public static void main(String[] args) {
		int nums = 0; 						// 用于控制记录奇数的个数
		int i = 1;
		System.out.println("1~100之间的奇数为:");
		do {
			if (i % 2 == 1) { 				// 判断是否是奇数
				nums++;
				System.out.print(i + "  ");		// 是则将其输出
				if (nums % 5 == 0)
					System.out.println(); 	// 每5个奇数一行
			}
			i++;
		} while (i <= 100);
	}
}

 

实例30  求最大的随机数

public class DoWhileCycle_02 {
	public static void main(String[] args) {
		int max = 0;		// 表示最大值
		int i = 0;			// 循环的次数
		int n1 = 0;		// 随机数
		System.out.println("随机生成的10个随机数分别为:");
		do {
			n1 = (int) (Math.random() * 100);	// 通过Math类random的产生0~99之间的随机正整数
			if (i == 0) {						// 如果是第一次循环
				max = n1;					// 则最大值为当前随机数
		// 否则,则将当前随机数与max的变量值进行比较,将最大值存放在max变量中
			} else if (n1 > max) {
				max = n1;
			}
			i++;							// 循环次数自动加1
			System.out.print(n1 + "  ");
		} while (i < 10);
		System.out.println("\n\n值最大的数字为:" + max);
	}
}

 

实例31  判断字母分类

import java.io.IOException;

public class SwitchCycle_01 {
	public static void main(String[] args) throws IOException { 	// 因为调用read()方法而抛出的IO异常
		System.out.println("请从键盘输入一个小写字母:");
		char ch = (char) System.in.read();					// 得到从键盘输入的字符
		int num = (int) ch; 		// 将char型强制转换成int,目的是根据哈希码值来判断是否是小写字母
		if (num < 97 || num > 122) { 	// a~z的哈希码值:97~122
			System.out.println("您的输入有误,请输入正确的小写字母!!");
		} else {
			switch (ch) {			// 用switch语句来判断是否是元音字母(a,e,I,o,u)
			case 'a':
			case 'e':
			case 'i':
			case 'o':
			case 'u':
				System.out.println(ch + " 是元音字母");
				break;
			default:
				System.out.println(ch + " 是辅音字母");
			}
		}
	}
}

 

实例32  优良及差

public class SwitchCycle_02 {
	public static void main(String[] args) {
		int level;
		level = (int) (Math.random() * 100);
		switch (level / 10) {
		case 6:
			System.out.println(level + ":一个刚刚合格的分数,还需在努力哦!");
			break;
		case 7:
			System.out.println(level + ":一个评为良的分数,还要加把劲啊!");
			break;
		case 8:
			System.out.println(level + ":一个评为良好的分数,加油啊!");
			break;
		case 9:
			System.out.println(level + ":一个优秀分数,你好棒啊!");
			break;
		default:
			System.out.println(level + ":一个不合格的分数,要十分努力才行!");
			break;
		}
	}
}

 

实例33  打印任一年日历

import java.io.IOException;
import java.util.Scanner;


public class SwitchCycle_03 {
	static int year, weekDay; 						// 定义静态变量,以便其它类调用
	public static boolean isLeapYear(int year) { 		// 判断是否是闰年
		return ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0));
	}
	public static int firstWeekDayOfYear(int year) { 	// 计算该年第一天是星期几
		long day = year * 365;
		for (int i = 1; i < year; i++)
			if (isLeapYear(i)) 					// 判断是否是闰年
				day += 1;
		return (int) day % 7;
	}
	public static int getMonthOfDays(int month) { 		// 获取每个月的天数
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			return 31;							// 以上月份是31天
		case 4:
		case 6:
		case 9:
		case 11:
			return 30;							// 以上月份是30天
		case 2:
			if (isLeapYear(year)) 				// 判断是否是闰年
				return 29;						// 如果是闰年,2月份就是29天
			else
				return 28;						// 否则就是28天
		default:
			return 0;
		}
	}
	public static void showMonths() { 				// 呈现该年的12个月中的所有日期
		for (int m = 1; m <= 12; m++) 				// 逐一将12个月份打印出来
		{
			System.out.println(m + "月");
			System.out.println("Sunday  Monday  Tuesday  Wednesday  Thursday  Friday  Saturday");											// 每个月的星期数
			for (int j = 1; j <= weekDay; j++) {		// 按每个月第一天是星期几打印相应的空格
				System.out.print("         ");
			}
			int monthDay = getMonthOfDays(m); 	// 获取每个月的天数
			for (int d = 1; d <= monthDay; d++) {		// 将每个月的天数以一周七天的形式打印出来
				if (d < 10)
					System.out.print("  " + "0" + d + "     ");
				else
					System.out.print("  " + d + "     ");
				weekDay = (weekDay + 1) % 7; 	// 判断当天的第二天是星期几
				if (weekDay == 0) 				// 如果第二天是星期天,便换行。
					System.out.println();
			}
			System.out.println();
		}
	}
	public static void main(String[] args) throws IOException {
		System.out.print("请输入一个年份:");
		Scanner sc = new Scanner(System.in);		// 以下接受从控制台输入
		String str = sc.nextLine();
		year = Integer.parseInt(str);
		weekDay = firstWeekDayOfYear(year); 		// 计算该年第一天是星期几
		System.out.println("\n          " + year + "年          ");
		showMonths();
	}


}

 

实例34  一年四季的划分

public class SwitchCycle_04 {
	public static void main(String args[]) {
		int m = 8;
		String s;					// s表示季节
		switch (m) {				// 这里m变量表示月份
		case 12:
		case 1:
		case 2:
			s = "Winter";			// 冬季
			break;
		case 3:
		case 4:
		case 5:
			s = "Spring";			// 春季
			break;
		case 6:
		case 7:
		case 8:
			s = "Summer";			// 夏季
			break;
		case 9:
		case 10:
		case 11:
			s = "Autumn";			// 秋季
			break;
		default:
			s = "Bogus Month";		// 这个月份是错误的
		}
		System.out.println("August is in the " + s + ".");
	}
}

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值