------Java培訓、Android培訓、iOS培訓、.Net培訓、期待與您交流! -------
1 if語句與三元運算符
1.1 第一種格式
public static void if1(){
int x = 3;
//if後面必須是條件表達式結果爲boolean類型
if(x>1){
System.out.println("yes");
}
System.out.println("over");
//如果if控制的只有一條語句那麼括號可以省略,
if (x<1)
System.out.println("HO");//if只控制裏他最近的一條語句,如果要控制多條語句要用{}括起來
System.out.println("NO");//該語句跟if沒有關係,注意代碼的層次感,不要將代碼縮進
}
1.2 第二種格式
public static void if2(){
int y = 4;
if(y>1){
System.out.println("yes");
}else{
System.out.println("over");
}
}
對於運算完有結果直接輸出的可以用三元運算符來寫
1.3 三元運算符(問號表達式)
好處:可以簡化if
else代碼。 if else 結構簡寫格式:變量 = (條件表達式)?表達式1:表達式2;
弊端:因爲是一個運算符,所以運算完必須要有一個結果
如:
String y;
y = (4>7)?"yes":"no";
System.out.println(y);
int a=9,b;
b = (a>1)?100:300;
if(a>1)
b=100;
else
b=300;
//這種運算完有結果的可以用三元運算符比較簡單
public class twentyNine {
public static void main(String[] args) {
int num = 5,b;
b = num>9? ++num : --num;
System.out.println(b);
Scanner input = new Scanner(System.in);
int score = input.nextInt();
String s = score>=60? "及格" : "不及格";
System.out.println("你的成績:"+s);
}
}
1.4 第三種格式
public static void if3(){
int n = 3;
if(n>1)
System.out.println("a");//只要有一個滿足程序就結束,都不滿足執行else最後結束
else if (n>2)
System.out.println("b");
else if (n>3)
System.out.println("c");
else
System.out.println("d");
}
2 while循環語句
爲了簡化代碼,讓代碼執行很多次
2.1 第一種格式:
while:先判斷條件,只有條件滿足才執行循環體
int x = 4;
while(x<8)//如果後面加了;號就會一直判斷x<8,(這裏是條件表達式,結果一定要是boolean類型,即結果一定要麼是真ture要麼是假false)
{
System.out.println("x="+x);
x++;
//System.out.println("x="+x++);//可以代替上面的 兩條語句 該句既可以打印出x的值又可以將x自增
}//沒加括號只能控制最近的一條語句
2.2 第二種格式:
do while:無論條件是否滿足先執行循環體一次後再判斷條件,條件滿足在繼續執行循環體(循環體至少執行一次)
int x = 4;
do{
System.out.println("x="+x);
x++;//x自增後變成5後,再執行下面判斷x是否小於8
}while (x<8);
3 for循環語句
for循環的格式:
for (初始化表達式;循環條件表達式 ;循環後的操作表達式 ){
執行語句
}
public class ForDemo {
public static void main(String[] args) {
//int x = 3;只有程序讀到這時纔在內存中開闢空間,而且只要for循環結束內存空間就會被釋放。
//第一個只要是合法的表達式就行,第二個一定要是條件表達式
for (int x = 3; x<8 ; x++ ){
System.out.println("x="+x);
//x只在該括號內有效
}
//System.out.println("x======="x);//編譯錯誤,找不到x這個變量
int y =3;
while(y<8)
{
System.out.println("y="+y);
}
System.out.println("y======="+y);//結果可以打印出y的值
/*
1.變量有自己的作用域,對應for來說,如果將用於控制循環的增量定義在for語句中,那麼該變量只在for語句中有效
for語句執行完畢,該變量在內存中被釋放
2.for和while可以進行互換,用哪個都可以,但是如果需要定義循環增量,用for合適些,比較節省內存
*/
}
}
for循環使用時常見的問題:
for (System.out.println("a");System.out.println("b") ;System.out.println("c") ){
System.out.println("d");
}
//沒有結果,因爲條件表達式既不是真也不是假所以沒結果
public class ForTest1 {
public static void main(String[] args) {
int x =1;
for (System.out.println("a");x<3 ;System.out.println("c") ){
System.out.println("d");
x++;//也可以放在for括號後面,用逗號隔開就行
}
//結果是:a d c d c
//for(這裏可以多表達式只要用,隔開)
}
}
無限循環的最簡單表現形式:
for (; ; ){}//對於for語句條件表達式只要不寫默認就是true
while(true){}
4 switch分支語句
4.1 swithc特點:只接受byte,shotr,int,char
語句的定義順序可以隨意的,但執行順序還是從第一個case開始
public class SwitchDemo {
public static void main(String[] args) {
int x = 4;
switch (x) {
case 1:
System.out.println("a");
break;
case 2:
System.out.println("b");
break;
case 3:
System.out.println("c");
break;
case 4:
System.out.println("d");
break;//遇到滿足條件的就執行該語句並跳出switch語句
default:
System.out.println("e");
break;//可以省略不寫,遇到括號就會結束
}
}
}
4.2 使用switch需注意的地方:
無論default放在什麼位置,運算都是從第一個case開始,所有case判斷完才執行default
所有case不符合,執行default,之後沒有break繼續執行下面的語句不再判斷case,只執行語句,直到遇到break或括號結束switch語句
public class SwitchDemo {
public static void main(String[] args) {
int x = 4;
switch(x)
{
default:
System.out.println("e");
//break;
case 1:
System.out.println("a");
//break;
case 2:
System.out.println("b");
break;
case 3:
System.out.println("c");
break;
}
}
}
4.3 與if語句的比較
什麼時候用if 什麼時候用switch(if和switch語句很像 具體什麼場景下 應用那個語句呢)
1、如果具體數值不多而且符合byte short int char這四種類型
2、雖然兩個語句都可以使用,建議使用switch語句,因爲效率稍高
3、其他情況:對區間判斷,對結果爲boolean類型判斷使用if if的使用範圍更廣
5 foreach循環語句:
5.1 foreach介紹
1、格式:
for(類型 變量名稱:被遍歷數組或集合){
其他操作(輸出操作)
}
2、作用:
主要是用於簡化書寫
5.2 foreach循環語句遍歷數組:
public class oneHundredAndEight_oneHundredAndNine {
public static void main(String[] args) {
String[] names = {"萌萌","純純","愛愛","鳳鳳","夢夢"};
for(String x:names){
System.out.print(x+" ");
}
}
}
public class ForEachDemo {
public static void main(String[] args) {
int[] arr = {3,5,6,7};
for (int i : arr ){
System.out.println(i);
}
}
}
5.3 foreach循環語句遍歷集合
List集合有兩種取出方式,一種是迭代器,和for循環,用get()方法按着角標取出元素
Set集合只有一種取出方式就是迭代器
凡是支持迭代器的集合都可以使用高級for
對集合進行遍歷,只能獲取元素,但是不能對集合進行過多的操作
迭代器除了遍歷還可以對集合中的元素進行remove
如果是ListIterator還可以在遍歷過程中對集合進行增刪改查的動作
import java.util.ArrayList;
public class ForEachDemo {
public static void main(String[] args) {
ArrayList<String> al = new ArrayList<String>();
al.add("abc2");
al.add("abc3");
al.add("abc1");
/*
for (Iterator<String> it = al.iterator() ;it.hasNext() ; ){
System.out.println(it.next());
}
*/
for(String s : al){
//s = "kk";//s不會改變集合中的元素,只能對集合中的元素取出,不能做修改動作
System.out.println(s);
}
System.out.println(al);//結果還是原集合
}
}
5.4 傳統for和高級for的區別:
1、傳統for:可以完成對語句執行很多次,因爲可以定義控制循環增量和條件
2、高級for有一個侷限性,必須有被遍歷的目標,而且可能會產生很多的垃圾,在一些內存比較緊張的項目中不建議使用,比如在安卓程序中,建議在遍歷數組的時候還是希望使用傳統for,因爲傳統for可以使用角標
3、高級for只能遍歷單例集合,要變量雙列集合(如Map集合),可以將Map集合變成單例集合再使用高級for
import java.util.HashMap;
public class ForEachDemo {
public static void main(String[] args) {
HashMap<Integer,String> hm = new HashMap<Integer,String>();
hm.put(1,"a");
hm.put(2,"a");
hm.put(3,"a");
//先用keySet或entrySet方法變成Set集合,在使用高級for
for (Integer key : hm.keySet()) {
String value = hm.get(key);
System.out.println(key+"...."+value);
}
}
}