天天看點

doWhile for switch

doWhile

doWhile for switch

package day3;

public class DoWhile {

public static void main(String[] args) {
	/**
	 * do while循環
	 * 格式 :do{}while(條件表達式)
	 * 特點:不管條件表達式是否滿足,循環體至少會執行一次
	 */
	int a=3;
	do {
		System.out.println(a);
		a--;
	}while(a<0);//3
           

// }while(a>0);//321

}
           

}

for

doWhile for switch

for 的死循環

doWhile for switch

for 1到100

doWhile for switch

for 嵌套

doWhile for switch

for的位置錯誤

doWhile for switch

正确位置

doWhile for switch

錯誤的位置 同一個數輸出了很多

package day3;

public class ForDemo {
			public static void main(String[] args) {
				/**
				 * for循環格式
				 * for (初始化表達式; 循環表達式; 循環後操作的表達式) {
				 * 執行的語句(循環體)
				 * 初始化表達式隻會執行一次,并且最開始執行
				 * for的執行流程:初始化表達式->循環表達式->循環後操作的表達式->
				 * 直到循環表達式不滿足
				}
				 */
				for(int i=0;i<6;i++) {
					System.out.println("循環體"+i);
				}
				int a=0;
				for(System.out.println("x");a<2;System.out.println("y")) {
					System.out.println("z");
					a++;
				}//xzyzy
		/*
		 * for(;;) { System.out.println("a"); } //無線循環
		 */
				//需求:1+2+3+....+100的和
				int sum=0;
				for(int i=1;i<=100;i++) {
					sum+=i;
				}
				System.out.println("sum="+sum);
			//嵌套for循環
			for(int i=3;i>0;i--) {
				for(int j=2;j>0;j--) {
					System.out.print(j+"");
				}
				System.out.println(i);
			}
		}
}
           

for列子以及簡化用法

doWhile for switch

switch break穿透和arg範圍

doWhile for switch

package day3;

import java.util.Scanner;

public class SwitchDemo {

public static void main(String[] args) {

/**

* 結構: 關鍵字:

* 格式:switch (表達式)

* { case 比對值:

* 比對成功執行的語句;

* break;

* default:

* 所有的case不能比對時執行的語句 ;

* break;

* }

/

int a=10;

String str=“aa”;

char c=‘中’;

//String JDK 1.7以後的

//int values, strings or enum variables,char

// switch© {

// case ‘中’:

// System.out.println(a);

// break;

// default:

// System.out.println(“沒有比對到”);

// break;

// }

//break 跳出本層循環

int b=1;

switch (b) {

case 1:

b++;

break;

case 2:

b++;

break;

case 3:

b++;

break;

case 4:

b++;

break;

case 5:

b++;

break;

case 6:

b++;

System.out.println(b);

break;

default:

System.out.println(“沒有比對到值”);

System.out.println(b);

break;

}

Scanner scanner=new Scanner(System.in);

System.out.println(“請輸入你的第一數:”);

int i=scanner.nextInt();

System.out.println(“請輸入你要計算得符号”);

String ch = scanner.next();

System.out.println(“請輸入你的第二數:”);

int j=scanner.nextInt();

// int i=10;

// int j=20;

switch (ch) {

case “+”:

System.out.println(“i”+ch+“j=”+(i+j));//+連接配接

break;

case “-”:

System.out.println(“i”+ch+“j=”+(i-j));//+連接配接

break;

case "":

System.out.println(“i”+ch+“j=”+(i*j));//+連接配接

break;

case “/”:

System.out.println(“i”+ch+“j=”+(i/j));//+連接配接

break;

default:

System.out.println(“沒有你想要的運算方式”);

break;

}

}

}