Java复习四

流程控制

复合语句:{}

条件语句 

//one
if(){
}
else{
}
//tow
if(){}
//three
if(){
}
else if(){
}
//four
switch(表达式){
case 常量1: {
}break;
case 常量2: {
}break;
case 常量3: {
}break;
default:
{}
[break;]
……
}

循环语句:

while(){
}

do{

}while();

for()

{
}

循环语句的跳出:continue,break

while循环计算1+1/2!+1/3!…1/20!

package first;

public class a {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     float sum = 0;
     int i=1;
     long j;
     int k;
     while(i<=20) {         
         j=1;
         for(k=1;k<=i;k++) {
             j*=k;
         }       
         sum+=(1.0/j);
         i++;
     }
     System.out.println(sum);
    }

}

应用if语句判断某一年是否为闰年

package first;

import java.util.Scanner;

public class a {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
     float sum = 0;
     int year;
     Scanner cin=new Scanner(System.in);
     year=cin.nextInt();
     if((year%100!=0&&year%4==0)||year%400==0)
           System.out.println("闰年");
     else
         System.out.println("平年");
    }

}

猜你喜欢

转载自www.cnblogs.com/gaochunhui/p/11027745.html