java中有哪些条件选择方法呢?
转自:
http://www.java265.com/JavaJingYan/202206/16557738473790.html
条件选择:
条件选择是日常开发中,常见的操作,那么到底有哪些条件选择方法呢,下文将一一道来,如下所示
下文笔者讲述java中条件选择方法分享,如下所示
if…else方法
实现思路:
通过不同的条件分支
选择不同的方法进行运行
例:
public class Test {
public static void main(String args[]){
int x = 8;
if( x < 30 ){
System.out.print("if 单条件判断");
}
}
}
----------------------------------------------------------------------
public class Test {
public static void main(String args[]){
int x = 8;
if( x < 10 ){
System.out.print("if 判断");
}else{
System.out.print("else 二次判断");
}
}
}
----------------------------------------------------------------------
public class Test {
public static void main(String args[]){
int x = 40;
if( x == 8 ){
System.out.print("第一判断条件");
}else if( x == 9 ){
System.out.print("第二判断条件");
}else if( x == 30 ){
System.out.print("第三判断条件");
}else{
System.out.print("最终判断条件");
}
}
}
switch case方法
通过switch case 进入不同的分支
例:
public class Test {
public static void main(String args[]){
char score = "F";
switch(score)
{
case "A" :
System.out.println("A成立");
break;
case "B" :
case "C" :
System.out.println("C成立");
break;
case "D" :
System.out.println("D成立");
break;
case "F" :
System.out.println("F成立");
break;
default :
System.out.println("默认成立");
}
}
}


