无声specialweek

从零开始学Java-Day04

分支结构

if

package cn.tedu.basic;

import java.util.Scanner;

//本类用于测试商品打折案例
public class TestDiscounut {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		System.out.println("请输入消费金额:");
		double money = scan.nextDouble();;
		if (money >= 10000) {
			System.out.println("打成骨折");
		}else if (money >= 5000) {
			money *= 0.5;
			System.out.println("打五折");
		}else if (money >= 2000) {
			money *= 0.9;
			System.out.println("打二折");
		}else {
			System.out.println("不打折");
		}
		System.out.println("需支付:" + money);
		
		scan.close();
	}
}

switch

int n = a;

switch(a){

​ case 1: System.out.println(1);break;

​ case 2: System.out.println(2);break;

​ case 3: System.out.println(3);break;

​ default:System.out.println("default");

}

package cn.tedu.basic;
//本类用于联系switch结构
/**
 * 总结:
 * 1.变量a的类型byte short char int String
 * 2.会拿着变量a的值依次与case后的值做比较,如果不加break
 * 会向后穿透,包括default
 * 3.如果设置default,在case无法匹配到a时,会输出default
 * 4.break和default是可选选项,根据具体业务需求添加
 * @author tarena
 *
 */
public class TestSwitch {
	public static void main(String[] args) {
		int a = 3;
		switch (a) {
		case 1:
			System.out.println(1);
		case 2:
			System.out.println(2);
		case 3:
			System.out.println(3);
		case 4:
			System.out.println(4);
		case 5:
			System.out.println(5);
		}
	}
}

package cn.tedu.basic;
//本类用于练习switch结构2
public class TestSwitch2 {
	public static void main(String[] args) {
		String  data = "星期四";
		switch (data) {
			case "星期一":System.out.println("今天星期一");break;
			case "星期二":System.out.println("今天星期二");break;
			case "星期三":System.out.println("今天星期仨");break;
			case "星期四":System.out.println("今天星期四");break;
			case "星期五":System.out.println("今天星期五");break;
			case "星期六":System.out.println("今天星期六");break;
			case "星期日":System.out.println("今天星期日");break;
			default :System.out.println("输入错误");
			
		}
	}
}

嵌套内循环

package cn.tedu.basic;
//本类用于测试循环for结构
public class TestFor {
	public static void main(String[] args) {
		for (int i = 1; i < 5; i++) {
			System.out.println("EMT!!!");
		}
		for (int i = 10; i > 0; i--) {
			System.out.print(i + " ");
		}
		
		System.out.println();
		System.out.println("============");
		
		for (int i = 0; i < 10; i++) {
			System.out.print(10 - i + " ");
		}
		System.out.println();
		System.out.println("============");
		int n = 0;
		int b = 0;
		for (int i = 8; i <= 8888; i = i*10 + 8) {
			System.out.print(i + " ");
		}
		
	}
}

package cn.tedu.basic;
//本类用于测试for循环结构2
public class TestFor2 {
	public static void main(String[] args) {
		m1();
		m2();
		m3();
	}
	
	//求1,100以内所有偶数的个数
	private static void m3() {
		int count = 0;
		for (int i = 2; i < 101; i+=2) {
			count ++;
		}
		System.out.println("100以内的偶数的个数" + count);
	}
	//求1,100以内所有偶数的和
	private static void m2() {
		int sum = 0;
		for (int i = 2; i < 101; i+=2) {
			sum += i;
		}
		System.out.println("100以内的偶数和" + sum);
	}
	//求1,100以内所有数的和
	private static void m1() {
		int sum = 0;
		for (int i = 1; i < 101; i++) {
			sum += i;
		}
		System.out.println("100以内的和" + sum);
	}
}




![](https://img2020.cnblogs.com/blog/2398441/202106/2398441-20210604115654814-881360144.png)

posted on 2021-06-04 11:58  无声specialweek  阅读(34)  评论(0编辑  收藏  举报

导航