國內半導體測試機龍頭華峰測控創始人孫銑因病去世
阿新 • • 發佈:2021-06-07
-
主要用於省略陣列和集合
-
jdk5的特性
package day06; public class Demo01 { public static void main(String[] args) { int[] i = {0, 1 , 2, 3, 4, 5 }; for (int a = 0; a <= 5; a++) { System.out.println(i[a]); } System.out.println("=============="); for (int b :i){//和上面一樣只不過方便一點 System.out.println(b); } } }
break和continue
-
跳出迴圈break(辭職)
package day06; public class Demo02 { public static void main(String[] args) { for (int i = 0; i < 5; i++) { System.out.println(i); if (i ==3 ){ break;//到3的時候跳出for迴圈 } } System.out.println("123");//有輸出 0 1 2 3 123 } }
-
跳過,但是迴圈繼續continue(請假)
package day06; public class Demo02 { public static void main(String[] args) { for (int i = 0; i < 100; i++) { if (i % 10==0 ){ System.out.println(); continue;//跳過取摸等於10的,迴圈繼續 } System.out.print((i)+"\t");//就是沒了0和10 } } }
-
求質數
package day06; public class Demo03 { //求1到10的質數 public static void main(String[] args) { aa:for (int i = 1; i <= 10; i++) { for (int j =2; j <= i/2; j++){ //判斷是否是質數,從2到總數除於2 if (i %j == 0){ // 如果有其他因數就跳出 continue aa; //然後繼續回到有aa標籤的迴圈 } // } // System.out.print(i+" "); } } }
5行三角
package day06; public class Demo04 { //列印5行三角形 public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) {//1.列印5個空格 2.列印4個空格 System.out.print(" "); } for (int j = 1; j <= i; j++ ){ System.out.print("*"); //1.列印一顆星 2.列印2顆星 3.列印3顆星 } for (int j = 1; j < i; j++ ){// 2.列印一顆星 3.列印2顆星 System.out.print("*"); } System.out.println(); //1.換行 2.換行 3.換行 } } }
方法
System.out.println() 類 + 物件 + 方法
-
使用add方法
-
方法包含於類或物件
package day06; public class Demo05 { public static void main(String[] args) { int sum = 0; System.out.println(sum=add(3,5)); } public static int add(int a,int b){ return a+b; } }