java5循环结构一

public class jh_01_循环学习需要用到的知识点 {
	public static void main(String[] args) {
		int a = 1;// 把数值1赋值给int类型的a
		
		a ++;// 对自身加1;
		System.out.println(a);
		a --;
		System.out.println(a);
		
		// a <= 10 --- 成立. true
		System.out.println(a <= 10);
		
		// 字符串比较内容相等.
        String str = "xiaojiejie";
		
		String answer = "n";
		boolean result = "y".equals(answer);
		System.out.println("y".equals(answer));
		
//		nextInt();
		
		
//		if() {
//			
//		}else {

		
	}
	}

  

public class jh_02_为什么需要循环2_1 {
	public static void main(String[] args) {
		System.out.println("xiaojiejie");
		System.out.println("xiaojiejie");
		System.out.println("xiaojiejie");
		System.out.println("xiaojiejie");
		System.out.println("xiaojiejie");
		/*
		 * int i = 1
		 * 循环条件:-- 不到100遍。-- i <= 100
		 * 循环操作 :print();
		 * 1 :有要重复的事。
		 * System.out.println("xiaojiejie");
		 * 
		 * 2:重复的条件。什么时候重复,什么时候不重复。
		 * 
		 * 3:想办法 去。影响条件。
		 */
		
	}

}

  

public class jh_03_什么是while循环 {
	public static void main(String[] args) {
//		if (condition) {
//			
//		}
//		while (condition) {
//			
//		}
//		while (条件) {
//			// 重复干的事。
//		}
//		
//		while (循环条件) {
//			// 循环操作---循环体。
//		}
//		
//		while (不到100遍) {
//			// print()。
//		}
		int i = 1;
		while (i<=100) {
			// print()。
			System.out.println("xiaojiejie");
			i ++;
		}
		
	}

}

  

public class jh_04_输出100遍helloworld {
	public static void main(String[] args) {
//		int i = 1;
//		while (i<=100) {
//			// print()。
//			System.out.println("helloworld");
//			i ++;
			int i = 1;
			while (i<=100) {
				// print()。
				System.out.println("helloworld");
				i ++;
		
			
			
		}
	}	

}

  

import java.util.Scanner;

public class jh_05_使用while循环4_3 {
	    /*
		 * 老师每天检查张浩的学习任务是否合格,
		 * 如果不合格,则继续进行。
		 * 老师给张浩安排的每天的学习任务为:
		 * 上午阅读教材,学习理论部分,
		 * 下午上机编程,掌握代码部分。
		 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		/*
		 *  while(){
		 *  }
		 */
		String answer = "n";
		while ("n".equals(answer)) {
			System.out.println("上午");
			System.out.println("下午");
			System.out.println("是否合格?");
			answer = sc.next();
		}
		System.out.println("合格了");
	}

}

  

public class jh_06_while循环_现场编程 {
	/*
	 * 2012年培养学员25万人,
	 * 每年增长25%,请问按此增长速度,
	 * 到哪一年培训学员人数将达到100万人?
	 */
	public static void main(String[] args) {
		/*
		 * 1: 循环操作. num * (1 + 0.25) 年份加1
		 * 2: 循环条件. num <= 100;
		 * -- 套用while循环语法结构
		 */
		int year = 2012;
		int num = 25;
		while(num <= 100) {
			// num = (int) (num * (1+0.25));
			num *= (1 + 0.25);
			year ++;	
		}
		System.out.println(num);
		System.out.println(year);
		
	}

}

  

public class jh_07_计算100以内的偶数之和2_1 {
	public static void main(String[] args) {
		// 1 -- 10
		int i = 1;
		int sum = 0;
		while(i<=100) {
//			System.out.println(i);
			if(i % 2 == 0) {
				sum += i;
			}
			i++;
		}
		System.out.println(sum);
	}

}

  

import java.util.Scanner;

public class jh_08_学员操作_查询商品价格2_1 {
	public static void main(String[] args) {
			Scanner sc = new Scanner(System.in);
			System.out.println("**************");
			System.out.println("1:A 2:B 3 :C");
			System.out.println("**************");
			System.out.println("请选择编号:");
			int choose = sc.nextInt();
			// 根据choosez做判断。
			switch (choose) {
			case 1:
				System.out.println("A");
				break;
			case 2:
				System.out.println("B");
				break;
			case 3:
				System.out.println("C");
				break;

			default:
				break;
			}
			
			System.out.println();
			System.out.println("是否继续");
			String answer = sc.next();
			while("y".equals(answer)) {
				System.out.println("请选择编号:");
				choose = sc.nextInt();
				// 根据choosez做判断。
				switch (choose) {
				case 1:
					System.out.println("A");
					break;
				case 2:
					System.out.println("B");
					break;
				case 3:
					System.out.println("C");
					break;

				default:
					break;
				}
				// 判断以后,改变循环条件
				System.out.println("是否继续");
				answer = sc.next();
			}
			
			System.out.println("执行结束");
		                         
	}

}

  

import java.util.Scanner;

public class jh_09_为什么需要do_while循环 {
	/*
	 * 经过几天的学习,老师给张浩一道测试题,
	 * 让他先上机编写程序完成,
	 * 然后老师检查是否合格。
	 * 如果不合格,则继续编写。
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
//		System.out.println("上机编写程序");
//		System.out.println("检查是否合格?y/n");
//		String answer = sc.next();
//		while("n".equals(answer)) {
//			System.out.println("上机编写程序");
//			System.out.println("检查是否合格?y/n");
//			 answer = sc.next();
//		}
		 String answer = "";
		do {
			System.out.println("上机编写程序");
			System.out.println("检查是否合格?y/n");
			answer = sc.next();
		}while("n".equals(answer));
		
		
	}

}

  

public class jh_10_现场编程do_while {
	/*
	 * 使用do-while实现:
	 * 输出摄氏温度与华氏温度的对照表,
	 * 要求它从摄氏温度0度到250度,每隔20度为一项,
	 * 对照表中的条目不超过10条。
	 * 转换关系:华氏温度 = 摄氏温度 * 9 / 5.0 + 32
	 * 1摄氏度=33.8华氏度
	 */
	public static void main(String[] args) {
		int ss = 0;
		int count = 0;
		do {
//			华氏温度 = 摄氏温度 * 9 / 5.0 + 32
//					 
			double hs = ss * 9 / 5.0 + 32;
			System.out.println(ss + " = " + hs );
			count ++;
			ss += 20;
		}while(ss <= 250 && count < 10);
		
	}

}

  

public class jh_11_比较while和do_while {
	public static void main(String[] args) {
		/*
		 *1: 语法不同
		 *2: 执行次序不同
		 *3: 初始情况不满足循环条件时
		 *初始情况不满足循环条件时
		 *while循环一次都不会执行
		 *do-while循环不管任何情况都至少执行一次
		 */
//		while() {}
//		do {}while();
		
		int i = 1;
		int sum = 0;
		do {
			if(i % 2 == 0) {
				sum += i;
			}
			i++;
		}while(i<=100);
		System.out.println(sum);
		
		// 输出10次helloworld
//		int i = 1;
//		do {
//			System.out.println("helloworld");
//			i ++;
//		}while(i<=10);
		
		
//		int i = 1;
//		while(i<=10) {
//			System.out.println("helloworld");
//			i++;
	}

}

  

import java.util.Scanner;

public class jh_12_学员操作升级购物结算 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		// 商品名称
		String tx = "T恤";
		String wqx = "网球鞋";
		String wqp = "网球拍";

		// 单价
		int txPrice = 245;
		int wqxPrice = 570;
		int wqpPrice = 320;
		System.out.println("****************************");
		System.out.println();
		System.out.println("请选择商品编号:");
		System.out.println("1:" + tx + "\t2:" + wqx+"\t3:"+wqp );
		System.out.println("*****************************");
		System.out.println();
		// 声明一个变量表示回答的内容。
		String answer = "";
		// 声明一个变量用于存储,商品累加额。
		int sumMoney = 0;
		do {
			System.out.println("请输入购买的商品编号:");
			int goodsId= sc.nextInt();
			System.out.println("请输入购买的数量:");
			int goodsCount = sc.nextInt();
			
			// 判断 --- 键值对
			switch (goodsId) {
				case 1:
					System.out.println(tx+ "\t"+txPrice+"\t数量"+goodsCount+
							"\t合计: "+(txPrice*goodsCount));
					// 把钱累加起来。
					sumMoney += txPrice*goodsCount;
					break;
				case 2:
					System.out.println(wqx+ "\t"+wqxPrice+"\t数量"+goodsCount+
							"\t 合计:"+(wqxPrice * goodsCount));
					sumMoney += wqxPrice * goodsCount;
					break;
				case 3:
					System.out.println(wqp+ "\t"+wqpPrice+"\t数量"+goodsCount+
							"\t合计: "+(wqpPrice*goodsCount));
					sumMoney += wqpPrice * goodsCount;
					break;
		
				default:
					break;
			}
			System.out.println("是否继续:y/n");
			answer = sc.next();
		}while("y".equals(answer));// answer cannot be resolved to a variable
		
		// 定义一个变量表示折扣。
		double discount = 0.8;
		// 定义一个变量表示应付金额:
		double payMoney = sumMoney *discount;
		
		System.out.println("折扣:"+discount);
		System.out.println("应付金额:"+payMoney);
		System.out.println("实付金额:");
		int pay = sc.nextInt();
		System.out.println("找钱:" + (pay-payMoney));
	}

}

  

import java.util.Scanner;

public class jh_13_学员操作_升级菜单切换 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("**********************************");
		System.out.println();
		System.out.println("\t1: 客户信息管理");
		System.out.println("\t2: 购物结算");
		System.out.println("\t3:真情回馈");
		System.out.println("\t4: 注销");
		System.out.println("**********************************");
		int choose = 0;
		boolean flag = false;
		do {
			System.out.println("请选择,输入数字: ");
			choose = sc.nextInt();
			switch (choose) {
			case 1:
				System.out.println("客户信息管理");
				flag = false;
				break;
			case 2:
				System.out.println("购物结算");
				break;
			case 3:
				System.out.println("真情回馈");
				flag = false;
				break;
			case 4:
				System.out.println("注销");
				flag = false;
				break;
			default:
				flag = true;
				break;
			}
			
		}while(flag);
		System.out.println("程序结束");
	}
//	do {
//		System.out.println("请选择,输入数字: ");
//		choose = sc.nextInt();
//		switch (choose) {
//		case 1:
//			System.out.println("客户信息管理");
//			break;
//		case 2:
//			System.out.println("购物结算");
//			break;
//		case 3:
//			System.out.println("真情回馈");
//			break;
//		case 4:
//			System.out.println("注销");
//			break;
//			
//		default:
//			break;
//		}
//		
//	}while(choose != 1 && choose != 2 && choose != 3 && choose != 4);
//}
	

}

  

原文地址:https://www.cnblogs.com/weiamd/p/11151522.html