java中有哪些条件选择方法呢?
下文笔者讲述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("默认成立");
}
}
}
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。


