Java流程控制:选择结构
发布日期:2021-05-15 03:50:09 浏览次数:15 分类:博客文章

本文共 5259 字,大约阅读时间需要 17 分钟。


������������������

  • ������������������������������������������������������������������������������������������
  • Java������������������������������������'if...else'���������'switch...case'���������
  • Java������������������������������������������������������������������������������������������������������������


������'if...else'������

���������'if'���������������������'else'���������������������Java���������'if...else'���������������������������������

���������

  1. 'if'���������������
  2. 'if'���������������
  3. 'if'���������������


������'if'���������������

���������

if(/* ��������������� */){	/* ������������������������true������������������ */}

���������

  • ������'if'������������������������������������������������������������
  • ������������������������������'true'������������'if'������������������������������������'if'���������������������������

���������

/* ���������������������������������������if������ */public static void main(String args[]){    int x = 10;    if(x < 20){    	System.out.print("������if������");    }}


������'if'���������������

���������

if(/* ��������������� */){	/* ������������������������������true������������������ */}else{	/* ������������������������������false������������������ */}

���������

  • 'if'���������������������'else'������������'if'������������������������������'false'������'else'������������������������

���������

/* ���������������������������������������else������ */public static void main(String args[]){    int x = 30;    if(x < 20){    	System.out.print("������if������");    }else{        System.out.print("������else������");    }}


������'if'���������������

���������

if(/* ���������������1 */){	/* ���������������������1���������true��������������������� */}else if(/* ���������������2 */){	/* ���������������������1���������false���������������������2���������true��������������������� */}else if(/* ���������������3 */){	/* ���������������������1������������������2������������false���������������������3���������true��������������������� */}else{	/* ���������������������1������������������2������������������3������������false��������������������� */}

���������

  • 'if'���������������������'else if���else'���������������'else if'������������������������������������������������������������������������������

���������

  • 'if'��������������� 1 ���'else'���������'else'������������������'else if'���������������
  • 'if'������������������������'else if'������������������������'else'���������������
  • ������������������'else if'���������'if'���������������true������������'else if'������'else'���������������������������

���������

/* ������������������������������������������������if else������ */public static void main(String args[]){    int x = 30;    if(x < 20){    	System.out.print("������if������");    }else if(x < 30){        System.out.print("������if else������");    }else if(x < 40){        System.out.print("���������������if else������");    }else{        System.out.print("������else������");    }}


������'switch'���������������

���������

switch(variable){	case value1:		/* ������������������value1��������������������������� */		break;/* break������������������ */	case value2:		/* ������������������value2��������������������������� */		break;	case value3:		/* ������������������value3��������������������������� */		break;	default:/* default������������������ */		/* ������������������value1���value2���value3������������������������������ */}

���������

  • 'switch...case'������������������������������������������������������������������������������������������������

���������

  • 'switch...case'���������������������������������byte���short���int���char������JavaSE7���������'switch...case'������������String������������������case���������������������������������������������
  • 'switch'������������������������'case'���������������'case'������������������������������������������'case'���������������������������������������������������������������������������������������������������������������
  • ������������������'case'������������������������������'case'������������������������������������������'break'������������������������'switch'���������������'switch'���������������������������
  • 'case'���������������������������'break'���������������������'break'���������������������������������������������'case'���������������������'break'���������
  • 'case'���������������������������������
  • 'switch'������������������������'default'���������������������������������'switch'������������������������������������������������������������������������������������'default'���������'case'������������������������������������������������'default'���������������'break'���������
  • 'switch...case'������������������������������������������������������������������������������������������������������������'break'������������������������������������������������������������������������������'switch...case'���������

���������

/* ���������������������������������������1 2 3 */public static void main(String args[]){    int i = 1;    switch(i){        case 0:            System.out.println("0");        case 1:            System.out.println("1");        case 2:            System.out.println("2");        case 3:            System.out.println("3");            break;        default:            System.out.println("default");    }}


上一篇:Java流程控制:循环结构
下一篇:Java流程控制:顺序结构

发表评论

最新留言

表示我来过!
[***.240.166.169]2025年05月02日 19时09分35秒