Java蛍屶陣崙
<貧匯准
和匯准>
Java嗤曾嶽蛍屶潤更?
聞喘if?else if?else囂鞘議扮昨?俶勣廣吭和中叱泣?
和匯准麼勣僥楼Java囂冱嶄Number窃?壓java.lang淫嶄?參式万議徨窃。
厘断繍僥楼泌採聞喘宸乂窃?遇音頁聞喘坪崔議方象窃侏?宸乂窃辛參喘栖斤方忖序佩鯉塀晒?賜宀啜嗤方僥侃尖議屢購圭隈
- if囂鞘
- switch囂鞘
if囂鞘?
匯倖if囂鞘淫根匯倖下櫛燕器塀才匯訳賜謹訳囂鞘。喘隈?
If囂鞘議喘隈泌和?
if(Boolean_expression){
//Executes when the Boolean expression is true
}else{
//Executes when the Boolean expression is false
}
泌惚下櫛燕器塀議峙葎true?夸峇佩if囂鞘嶄議旗鷹翠。倦夸峇佩If囂鞘翠朔中議旗鷹。
箭徨?
public class Test {
public static void main(String args[]){
int x = 10;
if( x < 20 ){
System.out.print("This is if statement");
}
}
}
塰佩潤惚泌和?
This is if statement
if…else囂鞘?
if囂鞘朔中辛參効else囂鞘?輝if囂鞘議下櫛燕器塀峙葎false扮?else囂鞘翠氏瓜峇佩。喘隈?
if…else議喘隈泌和?
if(Boolean_expression){
//Executes when the Boolean expression is true
}else{
//Executes when the Boolean expression is false
}
箭徨?
public class Test {
public static void main(String args[]){
int x = 30;
if( x < 20 ){
System.out.print("This is if statement");
}else{
System.out.print("This is else statement");
}
}
}
塰佩潤惚泌和?
This is else statement
if…elseif…else囂鞘?
if囂鞘朔中辛參効elseif…else囂鞘?宸嶽囂鞘辛參殊霞欺謹嶽辛嬬議秤趨。聞喘if?else if?else囂鞘議扮昨?俶勣廣吭和中叱泣?
- if囂鞘崛謹嗤1倖else囂鞘?else囂鞘壓侭嗤議elseif囂鞘岻朔。
- If囂鞘辛參嗤飛孤倖elseif囂鞘?万断駅倬壓else囂鞘岻念。
- 匯稀凪嶄匯倖else if囂鞘殊霞葎true?凪麿議else if參式else囂鞘脅繍柳狛峇佩。
喘隈?
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
}else if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}else if(Boolean_expression 3){
//Executes when the Boolean expression 3 is true
}else {
//Executes when the none of the above condition is true.
}
箭徨?
public class Test {
public static void main(String args[]){
int x = 30;
if( x == 10 ){
System.out.print("Value of X is 10");
}else if( x == 20 ){
System.out.print("Value of X is 20");
}else if( x == 30 ){
System.out.print("Value of X is 30");
}else{
System.out.print("This is else statement");
}
}
}
塰佩潤惚泌和?
Value of X is 30
廼耗議if…else囂鞘?
聞喘廼耗議if-else囂鞘頁栽隈議。匆祥頁傍低辛參壓総匯倖if賜宀elseif囂鞘嶄聞喘if賜宀elseif囂鞘。喘隈?
廼耗議if…else喘隈泌和?
if(Boolean_expression 1){
//Executes when the Boolean expression 1 is true
if(Boolean_expression 2){
//Executes when the Boolean expression 2 is true
}
}
箭徨?
public class Test {
public static void main(String args[]){
int x = 30;
int y = 10;
if( x == 30 ){
if( y == 10 ){
System.out.print("X = 30 and Y = 10");
}
}
}
塰佩潤惚泌和?
X = 30 and Y = 10
switch囂鞘?
switch囂鞘登僅匯倖延楚嚥匯狼双峙嶄蝶倖峙頁倦屢吉?耽倖峙各葎匯倖蛍屶。喘隈?
switch喘隈泌和?
switch(expression){
case value :
//Statements
break; //optional
case value :
//Statements
break; //optional
//You can have any number of case statements.
default : //Optional
//Statements
}
switch囂鞘嗤泌和号夸?- switch囂鞘嶄議延楚窃侏峪嬬葎byte、short、int賜宀char。
- switch囂鞘辛參啜嗤謹倖case囂鞘。耽倖case朔中効匯倖勣曳熟議峙才丹催。
- case囂鞘嶄議峙議方象窃侏駅倬嚥延楚議方象窃侏屢揖?遇拝峪嬬頁械楚賜宀忖中械楚。
- 輝延楚議峙嚥case囂鞘議峙屢吉扮?椎担case囂鞘岻朔議囂鞘蝕兵峇佩?岷欺break囂鞘竃孖嘉氏柳竃switch囂鞘。3
- 輝囑欺break囂鞘扮?switch囂鞘嶮峭。殻會柳廬欺switch囂鞘朔中議囂鞘峇佩。case囂鞘音駅倬勣淫根break囂鞘。泌惚短嗤break囂鞘竃孖?殻會氏写偬峇佩和匯訳case囂鞘?岷欺竃孖break囂鞘。
- switch囂鞘辛參淫根匯倖default蛍屶?乎蛍屶駅倬頁switch囂鞘議恷朔匯倖蛍屶。default壓短嗤case囂鞘議峙才延楚峙屢吉議扮昨峇佩。default蛍屶音俶勣break囂鞘。
箭徨?
public class Test {
public static void main(String args[]){
char grade = args[0].charAt(0);
switch(grade)
{
case 'A' :
System.out.println("Excellent!");
break;
case 'B' :
case 'C' :
System.out.println("Well done");
break;
case 'D' :
System.out.println("You passed");
case 'F' :
System.out.println("Better try again");
break;
default :
System.out.println("Invalid grade");
}
System.out.println("Your grade is " + grade);
}
}
園咎貧中議殻會?聞喘凋綜佩勧弓公延楚峙。塰佩潤惚泌和?
$ java Test a Invalid grade Your grade is a a $ java Test A Excellent! Your grade is a A $ java Test C Well done Your grade is a C $和匯准勣讐議坪否?
和匯准麼勣僥楼Java囂冱嶄Number窃?壓java.lang淫嶄?參式万議徨窃。
厘断繍僥楼泌採聞喘宸乂窃?遇音頁聞喘坪崔議方象窃侏?宸乂窃辛參喘栖斤方忖序佩鯉塀晒?賜宀啜嗤方僥侃尖議屢購圭隈
<貧匯准
和匯准>