本篇介绍Java的条件语句:if..else语句以及switch..case语句。
package com.example.javatest;
/**
* Author:W
* 条件语句
* 1.if..else语句
* 2.switch..case语句
*/
public class IfTest {
//if..else语句
public void method1()
{
System.out.println("===if..else if..else===");
int a = 10;
if (a < 10)
{
System.out.println("a<10");
}else if (a == 10)
{
System.out.println("a=10");
}else
{
System.out.println("a>10");
}
}
//嵌套if..else语句
public void method2()
{
System.out.println("===嵌套if..else语句===");
int a = 3;
int b = 5;
if (a > 2)
{
if (b > 4)
{
System.out.println("a>2 且 b>4 ");
}
}
}
//switch..case语句
public void method3()
{
System.out.println("===switch..case语句===");
int a = 5;
switch (a)
{
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
System.out.println("a=5");
break;
default:
break;
}
}
}
测试脚本
package com.example.javatest;
/*
*Author:W
* 运算符测试
*/
public class MainTest {
public static void main(String[] args)
{
IfTest ifTest = new IfTest();
ifTest.method1();
ifTest.method2();
ifTest.method3();
}
}
运行结果如下: