条件语句

Java有以下几种分支结构:

  • if
  • if...else
  • if...else if....else
  • switch...case...default

if...else

示例代码

int x = 10;
if( x < 10 ){
    System.out.print("x<10");
}else if(x==10) {
    System.out.print("x==10");
}else{
    System.out.print("x>10");
}

switch...case...default

示例代码

char grade = 'C';
switch(grade)
{
  case 'A' :
    System.out.println("优秀");
    break;
  case 'B' :
  case 'C' :
    System.out.println("良好");
    break;
  case 'D' :
    System.out.println("及格");
  case 'F' :
    System.out.println("你需要继续努力");
    break;
  default :
    System.out.println("无效等级");
    break;
}

MIT Licensed | Copyright © 2018-present 滇ICP备16006294号

Design by Quanzaiyu | Power by VuePress