java从入门到精髓 - 流程控制
时间:2010-08-25 来源:叮当小马
public class GetIfElse {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
//if语句
int x = 45;
int y = 12;
if(x>y)
{
System.out.println("变量x>y");
}
if(x<y)
{
System.out.println("x<y");
}
//if else语句
int z = 20;
if(z>30)
{
System.out.println("z>30");
}else if(z>10)
{
System.out.println("10<z<30");
}else if(z>0)
{
System.out.println("0<z<10");
}else
{
System.out.println("z<0");
}
//switch case
int week = 2;
switch(week)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;
case 3:
System.out.println("Wednesday");
break;
default:
System.out.println("Sorry ,I don't know.");
}
//while语句
int xx =1;
int sum = 0;
while(xx<=10)
{
sum = sum + xx;
xx++;
}
System.out.println("sum = "+ sum);
//do ..while
int a = 100;
while(a==60)
{
System.out.println("ok1");
a--;
}
int b = 100;
do
{
System.out.println("ok2");
b--;
}while (b==60);
//for语句
int summ = 0;
for(int i = 2;i<=100;i+=2)
{
summ = summ +i;
}
System.out.println("2-100所有的偶数和:" + summ );
//无限循环:
int w=0;
for(;;)
{
if(w<20)
break;
w++;
}
//for each 语句
int arr[] = {7,10,1};
System.out.println("一维数组中的元素为:");
for(int q : arr)
{
System.out.println(q);
}
}
}