标签:style blog color io 使用 java ar for div
格式:
if(boolean - expresion){
statement
}
或
if(boolean - expresion){
statement
}else{
statement
}
例:
public static int testIf(int testVal,int target){ int result = 0; if(testVal > target){ result+=1; }else if(testVal<target){ result -=1; }else{ result = 0; } return result; } public static void main(String[] args) { int test1 = testIf(10, 9); System.out.println(test1); // 1 }
1 while循环格式: 2 while(boolean - expresion){ 3 statement 4 } 5 while循环求阶乘: 6 int i1 = 1, sum1 = 0 , jc1 = 1; 7 while(i1<=10){ 8 jc1 = jc1 * i1; 9 sum1 = sum1 + jc1; 10 i1++; 11 } 12 System.out.println(sum1); //4037913
1 do-while循环格式: 2 do{ 3 statement 4 }while(boolean-expresion); 5 6 do-while循环求阶乘: 7 int i2 = 1, sum2 = 0 , jc2 = 1; 8 do{ 9 jc2 = jc2 * i2; 10 sum2 = sum2 + jc2; 11 i2++; 12 }while(i2<=10); 13 System.out.println(sum2); //4037913
while语句与do-while语句唯一区别是do-while语句至少会执行一次,即使do-while循环表达式第一次计算为FALSE,也会执行一次,而while循环第一次若计算为FALSE,而其语句根本不会执行
1 for循环求阶乘: 2 int i3, sum3 = 0 , jc3 = 1; 3 for(i3 = 1;i3 <= 10;i3++){ 4 jc3 = i3 * jc3; 5 sum3 += jc3; 6 } 7 System.out.println(sum3);//4037913
1 例:9X9乘法表 2 方法一:循环嵌套(for switch) 3 public static void main(String args[]){ 4 //9x9乘法表 5 6 int i,j; 7 //外层控制行,内层控制列 8 System.out.println("***************方法一:******************"); 9 for(i = 1;i<=9;i++){ 10 for(j = 1 ; j <= 9;j++){ 11 switch (j) { 12 case 1: 13 System.out.print(j+"x"+i+"="+i*j); 14 break; 15 case 2: 16 if(i>=2){ 17 System.out.print("\t"+j+"x"+i+"="+i*j); 18 break; 19 } 20 case 3: 21 if(i>=3){ 22 System.out.print("\t"+i+"x"+j+"="+i*j); 23 break; 24 } 25 case 4: 26 if(i>=4){ 27 System.out.print("\t"+i+"x"+j+"="+i*j); 28 break; 29 } 30 case 5: 31 if(i>=5){ 32 System.out.print("\t"+i+"x"+j+"="+i*j); 33 break; 34 } 35 case 6: 36 if(i>=6){ 37 System.out.print("\t"+i+"x"+j+"="+i*j); 38 break; 39 } 40 case 7: 41 if(i>=7){ 42 System.out.print("\t"+i+"x"+j+"="+i*j); 43 break; 44 } 45 case 8: 46 if(i>=8){ 47 System.out.print("\t"+i+"x"+j+"="+i*j); 48 break; 49 } 50 case 9: 51 if(i>=9){ 52 System.out.print("\t"+i+"x"+j+"="+i*j); 53 break; 54 } 55 } 56 } 57 System.out.println(""); 58 } 59 System.out.println("***************方法二:*********************"); 60 int i1,j1; 61 for(i1 = 1;i1<=9;i1++){ 62 for(j1 = 1 ; j1 <= i1;j1++){ 63 System.out.print(i1+"x"+j1+"="+i1*j1+"\t"); 64 } 65 System.out.println(""); 66 } 67 } 68 }
标签:style blog color io 使用 java ar for div
原文地址:http://www.cnblogs.com/Essence/p/3961861.html