小白入门第七弹 编程逻辑

(1)程序的执行顺序:
Java从入口程序main开始执行;
程序子上而下顺序执行;
遇到放到调用,程序跳转到方法调用处继续执行,方法执行完毕,再跳转到调用方法处;
(2)Java循环:
for/for each

package com.csdn;

public class Demo07 {
	static void m1() {
		int[] a = {1, 2, 3, 4};
		for (int i : a) {
			System.out.println(i);
		}
	}
	static void testFor() {
		int[] a = {1, 2, 3, 4};
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}
	public static void main(String[] args) {
		testFor();
//		System.out.println("star...");
//		m1();
//		System.out.println("end...");
	}
}
package com.csdn;

public class Demo07 {
	static void m1() {
		int[] a = {1, 2, 3, 4};
		for (int i : a) {
			System.out.println(i);
		}

	static void testFor() {
		int[] a = {1, 2, 3, 4};
//		for (int i = 0; i < a.length; i++) {
//			System.out.println(a[i]);
//		}
		//直接显示数组中的元素,不需要利用下标
		for (int i : a) {
			System.out.println(i);
		}
	}
	public static void main(String[] args) {
		testFor();
//		System.out.println("star...");
//		m1();
//		System.out.println("end...");
	}
}

while/do while

package com.csdn;

public class Demo07 {
	static void m1() {
		int[] a = {1, 2, 3, 4};
		for (int i : a) {
			System.out.println(i);
		}
	}
	static void testFor() {
		int[] a = {1, 2, 3, 4};
//		for (int i = 0; i < a.length; i++) {
//			System.out.println(a[i]);
//		}
		//直接显示数组中的元素,不需要利用下标
//		for (int i : a) {
//			System.out.println(i);
//		}
	}
	static void testWhile() {
		int sum = 0;
		int i = 1;
		while (i<=10) {
			sum +=i;
			i++;
		}
		System.out.println(sum);
	}
	static void testDoWhile() {
		int sum = 0;
		int i = 1;
		do {
			sum = sum + i; 
			i++;
		}while(i<=10);
		System.out.println(sum);
	}
	
	static void testContinue() {
		for(int i=1; i<=10;i++) {
			if (i%2==0) {
				System.out.println(i);
			}else {
				continue;
//				System.out.println("here...");
			}
		}
	}
	public static void main(String[] args) {
		testDoWhile();
		//		testWhile();
		//testFor();
//		System.out.println("star...");
//		m1();
//		System.out.println("end...");
	}
}

break/continue 关键字

package com.csdn;

public class Demo07 {
	static void m1() {
		int[] a = {1, 2, 3, 4};
		for (int i : a) {
			System.out.println(i);
		}
	}
	static void testFor() {
		int[] a = {1, 2, 3, 4};
//		for (int i = 0; i < a.length; i++) {
//			System.out.println(a[i]);
//		}
		//直接显示数组中的元素,不需要利用下标
//		for (int i : a) {
//			System.out.println(i);
//		}
	}
	static void testWhile() {
		int sum = 0;
		int i = 1;
		while (i<=10) {
			sum +=i;
			i++;
		}
		System.out.println(sum);
	}
	static void testDoWhile() {
		int sum = 0;
		int i = 1;
		do {
			sum = sum + i; 
			i++;
		}while(i<=10);
		System.out.println(sum);
	}
	//continue后的代码将不会执行,表示终止本次循环,进行下一次循环
	static void testContinue() {
		for(int i=1; i<=10;i++) {
			if (i%2==0) {
				System.out.println(i);
			}else {
				continue;
//				System.out.println("here...");
			}
		}
	}
	//循环直接中断
	static void testBreak() {
		for(int i=1; i<=10;i++) {
			if (i%2==0) {
				System.out.println(i);
			}else {
				break;
//				System.out.println("here...");
			}
		}
	}
	public static void main(String[] args) {
		testBreak() ;
		//		testContinue();
		//		testDoWhile();
		//		testWhile();
		//		testFor();
//		System.out.println("star...");
//		m1();
//		System.out.println("end...");
	}
}

(3)Java分支:
if else
if else if else
switch

package com.csdn;

public class Demo08 {
	static void testSwitch() {
		char grade = 'A';
		switch (grade) {
		case 'A':
			System.out.println("优秀");
			break;
		case 'B':
		case 'C':
			System.out.println("良好");
			break;
		case 'D':
			System.out.println("一般");
			break;
		default:
			System.out.println("不及格");
			break;
		}
	}
	static void IfElse2() {
		int a = 10;
		if (a==20) {
			System.out.println("20");
		}else if(a==30) {
			System.out.println("30");
		}else if(a==40) {
			System.out.println("40");
		}else {
			System.out.println("other");
		}
	}
	static void testIfElse() {
		int i = 1;
		if (i>10) {
			System.out.println("大于10");
		}else {
			System.out.println("小于10");
		}
	}
	static void testIf() {
		boolean flag = false;
		if(flag) {
			System.out.println("here");
		}
		System.out.println("end...");
	}
	public static void main(String[] args) {
//		testIf();
//		testIfElse();
//		IfElse2();
		testSwitch();
		}
}

以上的代码块仅供参考,建议在参考代码的同时一定要有相应的基础知识,不要盲目参考复制代码,我们一起努力!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

固执的鱼besos

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值