if (条件表达式)
{
语句1
}
Else
{
语句2
}
语句3
if(a>5) b=1
else if(a>4) b=2
else if(a>3) b=3
...
else b=-1;
switch(条件表达式)
{
case 常量表达式1:
语句1 ;
break ;
case 常量表达式2:
语句2 ;
break ;
//......
case 常量表达式n:
语句n ;
break ;
default:
//
break
}
class Switch Test
public static void main(String args[])
throws java.io.IOException
char a;
System.out.PRintln("Enter a number from 1--3:");
a=(char)System.in.read();
switch(a)
case'1':System.out.println("win a Car!");break;
case'2':System.out.println("picked the goat");break
case'3':System.out.println("get to keep your 100");
break;
default:System.out.println("entry");
class SwitchTest
public static void main (String args[])
throws java.io.IOException
int numberOfDigits=0,numberOfSpaces=0,numberOfOthers=0;
char c;
while((c=(char)System.in.read())!='')
switch(c)
case'0':
case'1':
...
...
case'8':
case'9':numberOfDigits++;break;
case' ':numberOfSpaces++;break;
default:numberOfOthers++;break;
System.out.println(":");
System.out.println("Number of digits="+numberOfDigits+"");
System.out.println("Number of spaces="+numberOfSpaces+"");
System.out.println("Number of others="+numberOfOthers+"");
新闻热点
疑难解答