任何一門知識的簡歷都是在原有知識的基礎上開發建立的。
Java繼承發揚C++,C++繼承發揚C語言,C語言繼承發揚B語言,B語言繼承發揚匯編語言,匯編語言又是建立在數學的分支上。
所以編程語言有很多數學基礎。數學學得好,特別是數據結構與算法學的好,更加有助於學習精深編程。
同樣我們的說話中也包含了很多邏輯數學知識。
就比如if條件判斷語句。
如果(怎么怎么樣)則{怎么怎么樣;}否則{怎么怎么樣;}
If(條件判斷){為真則表達式1}else{為假則表達式2}。
又因為世上的情況不止一兩種,所以進一步:
If(條件判斷){為真則表達式1}
else if(條件判斷){為真則表達式2}
else if(條件判斷){為真則表達式3}
else if(條件判斷){為真則表達式3}
……
else if(條件判斷){為真則表達式n}
else{剩余所有情況下執行則表達式n+1}。


![]()

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = in.nextInt();
if (score > 100 || score < 0) {
System.out.println("成績輸入有誤,請重新輸入");
}
if (score >= 90 && score <= 100) {
System.out.println("優秀");
}
if (score >= 80 && score < 90) {
System.out.println("良好");
}
if (score >= 60 && score < 80) {
System.out.println("及格");
}
if ( score >= 0&&score < 60) {
System.out.println("不及格");
可以簡化為if(){}else if(){} else{}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = in.nextInt();
if (score > 100 || score < 0) {
System.out.println("成績輸入有誤,請重新輸入");}
else if(score >= 90 ) {
System.out.println("優秀");
}
else if (score >= 80 ) {
System.out.println("良好");
}
else if (score >= 60 ) {
System.out.println("及格");
}
else if ( score >= 0) {
System.out.println("不及格");
}
}
我們還可以繼續簡化。等號相當於特殊的大於>小於<情況。
如果及格不同的區間轉化為幾個不同的特定取值。也就是將大小區間轉化為等於。
100=>A>=90。這一區間用A代替。
如果等於A,則優秀。
int a=(int)A/10;//則a=9or10.
90>A>=80
int a=(int)A/10;//a=8
80>A>=70
int a=(int)A/10;//a=7
70>A>=60
int a=(int)A/10;//a=6
代碼如下:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = in.nextInt();
int a = (int) score / 10;
if (a > 10 || a < 0) {
System.out.println("成績輸入有誤,請重新輸入");
}
if (a == 10 | a == 9) {
System.out.println("優秀");
} else if (a == 8) {
System.out.println("良好");
} else if (a == 7 || a == 6) {
System.out.println("及格");
} else if (a>=0&&a<6) {
System.out.println("不及格");
}
}
//這里面有一個bug,不知道大家注意到沒有。我也不知道怎么解決,看到家能否找到。提示一下:是負數取值區間的。
對於這種特殊的情況。Java里面用了另外一個語法來定義。那就是switch(條件){
case a:執行表達式
break;
case b:
break;
case c:
break;
}
//Switch()就是if的判斷表達式。Case就是判斷的結果。a 是整形數字。后面接着執行的表達式。再接着就是break。相當於原來的if改為else if。就是確定區間。只執行這一段程序,遇見則退出,不再執行其他的程序。
代碼如下:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("請輸入成績:");
int score = in.nextInt();
int a = (int) score / 10;
switch (score / 10) {
case 10:
case 9:
System.out.println("優秀");
break;
case 8:
System.out.println("良好");
break;
case 7:
case 6:
System.out.println("及格");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
System.out.println("不及格");
break;
default:
System.out.println("成績輸入有誤,請重新輸入");
}
}
//這樣一來代碼更加簡化了,而且可以用於等於==這種特殊的情況。不過負數取值區間的bug仍然存在。我還是無法解決。
希望大家能幫我解決它。謝謝。
任何一門知識的簡歷都是在原有知識的基礎上開發建立的。 Java繼承發揚C++,C++繼承發揚C語言,C語言繼承發揚B語言,B語言繼承發揚匯編語言,匯編語言又是建立在數學的分支上。 所以編程語言有很多數學基礎。數學學得好,特別是數據結構與算法學的好,更加有助於學習精深編程。 同樣我們的說話中也包含了很多邏輯數學知識。 就比如if條件判斷語句。 如果(怎么怎么樣)則{怎么怎么樣;}否則{怎么怎么樣;} If(條件判斷){為真則表達式1}else{為假則表達式2}。 又因為世上的情況不止一兩種,所以進一步: If(條件判斷){為真則表達式1} else if(條件判斷){為真則表達式2} else if(條件判斷){為真則表達式3} else if(條件判斷){為真則表達式3} …… else if(條件判斷){為真則表達式n} else{剩余所有情況下執行則表達式n+1}。 public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入成績:"); int score = in.nextInt(); if (score > 100 || score < 0) { System.out.println("成績輸入有誤,請重新輸入"); } if (score >= 90 && score <= 100) { System.out.println("優秀"); } if (score >= 80 && score < 90) { System.out.println("良好"); } if (score >= 60 && score < 80) { System.out.println("及格"); } if ( score >= 0&&score < 60) { System.out.println("不及格"); 可以簡化為if(){}else if(){} else{} public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入成績:"); int score = in.nextInt(); if (score > 100 || score < 0) { System.out.println("成績輸入有誤,請重新輸入");} else if(score >= 90 ) { System.out.println("優秀"); } else if (score >= 80 ) { System.out.println("良好"); } else if (score >= 60 ) { System.out.println("及格"); } else if ( score >= 0) { System.out.println("不及格"); } } 我們還可以繼續簡化。等號相當於特殊的大於>小於<情況。 如果及格不同的區間轉化為幾個不同的特定取值。也就是將大小區間轉化為等於。 100=>A>=90。這一區間用A代替。 如果等於A,則優秀。 int a=(int)A/10;//則a=9or10. 90>A>=80 int a=(int)A/10;//a=8 80>A>=70 int a=(int)A/10;//a=7 70>A>=60 int a=(int)A/10;//a=6 代碼如下: public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入成績:"); int score = in.nextInt(); int a = (int) score / 10; if (a > 10 || a < 0) { System.out.println("成績輸入有誤,請重新輸入"); } if (a == 10 | a == 9) { System.out.println("優秀"); } else if (a == 8) { System.out.println("良好"); } else if (a == 7 || a == 6) { System.out.println("及格"); } else if (a>=0&&a<6) { System.out.println("不及格"); } } //這里面有一個bug,不知道大家注意到沒有。我也不知道怎么解決,看到家能否找到。提示一下:是負數取值區間的。 對於這種特殊的情況。Java里面用了另外一個語法來定義。那就是switch(條件){ case a:執行表達式 break; case b: break; case c: break; } //Switch()就是if的判斷表達式。Case就是判斷的結果。a 是整形數字。后面接着執行的表達式。再接着就是break。相當於原來的if改為else if。就是確定區間。只執行這一段程序,遇見則退出,不再執行其他的程序。 代碼如下: public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("請輸入成績:"); int score = in.nextInt(); int a = (int) score / 10; switch (score / 10) { case 10: case 9: System.out.println("優秀"); break; case 8: System.out.println("良好"); break; case 7: case 6: System.out.println("及格"); break; case 5: case 4: case 3: case 2: case 1: case 0: System.out.println("不及格"); break; default: System.out.println("成績輸入有誤,請重新輸入"); } } //這樣一來代碼更加簡化了,而且可以用於等於==這種特殊的情況。不過負數取值區間的bug仍然存在。我還是無法解決。 希望大家能幫我解決它。謝謝。 JAVA
作者: 玄鑒
鏈接:http://www.imooc.com/article/7477
來源:慕課網
