if後面是布爾類型
表示兩個相等是用 equals
eg:sex=”女“;
if(sex.equals("女")).....
switch語句中,case後面要跟多個參數,例如:case 1:case 2:case 3:System.out.println("我餓了");berak;
while語句,先判斷再操作,do while語句是先操作再判斷(do while 語句需要無條件操作一次)
break:結束整個循環。continue:結束本次循環,看是否進入下一個循環
數組:int[] stu;(整型數組定義)
String[] stu;(字元型數組定義)
double[] stu;(浮點型數組定義)
數組名 = new 資料類型 [ 數組長度 ];(配置設定空間)
數組下标從0開始。
數組指派:eg:1、int[] num={1,5,6,7,8};
2、int[] num=new int[5];
num[0]=1;
num[1]=5;
..........
在System.out.println();在括号裡有加" "," "裡原樣輸出.eg:
public class cyb{
public static void main(String[] args) {
int classNum=3;
System.out.println(classNum);
}
}
結果:3
public class cyb{
public static void main(String[] args) {
int classNum=3;
System.out.println("classNum");
}
}
結果: classNum
Arrays.toString(數組名);【轉換為字元型】
Arrays.sort(數組名);【排序】
import java.util.Arrays;
public class cyb{
public static void main(String[] args) {
String[] subject = { "math", "chinese", "english" };
Arrays.sort(subject);
System.out.println("排序後數組中的元素:"+Arrays.toString(subject) );
}
}
foreach:
for(變量類型 變量:總對象)
public class cyb {
public static void main(String[] args) {
int[] scores = {90,89,91,75,94};
for(int i=0;i<scores.length;i++){
System.out.println(scores[i]);
}
//等價
}
}
數組替換
public class cyb {
public static void main(String[] args) {
int[] score = new int[] {1,4,3,87,100};
int max = score[0];
int min = score[0];
for (int i = 0; i < score.length; i++) {
if(score[i]>max){
max=score[i];
}
if(score[i]<min){
min=score[i];
}
}
System.out.println("數組中的最大值:" + max);
System.out.println("數組中的最小值:" + min);
}
}
二維數組
type[][] 變量={值1,值2,值3,...},{值1,值2,值3,...};(大括号代表行,值代表列)
或
type[][] 變量=new type[][];
變量[行][列]=值;