publicclassFor{ publicstaticvoidmain(String[] args){ for (int i=1;i<=100;i++){//在IDEA里输入100.for自动生成这一行 System.out.println(i); }
//计算0-100之间的奇数和偶数的和 int oddSum = 0; int evenSum = 0;
for (int i = 0; i <= 100; i++) { if (i%2!=0){ oddSum+=i; }else { evenSum+=i; } } System.out.println("奇数的和:"+oddSum); System.out.println("偶数的和:"+evenSum);
//输出1-1000之间能被5整除的数,并且每行输出3个 for (int i = 0; i <= 1000; i++) { if (i%5==0){ System.out.print(i+"\t"); } if (i%(5*3)==0){//每行 System.out.println(); //System.out.print("\n"); } } //println:输出完会换行 //print:输出完不会换行 System.out.println();
//打印九九乘法表 for (int i = 1; i <= 9; i++) { for (int j = 1; j <= i; j++) { System.out.print(j+"×"+i+"="+j*i+'\t'); } System.out.println(); } } }
publicclassBreakContinue{ publicstaticvoidmain(String[] args){ int i = 0; while (i<100){ i++; System.out.println(i); if (i==30){ break; } } i = 0; while (i<100){ i++; if (i%10==0){ System.out.println(); continue; } System.out.println(i); } } }
打印三角形
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassPrintTriangle{ publicstaticvoidmain(String[] args){ //打印三角形 5行 for (int i = 1; i <= 5; i++) { for (int j = 5; j >= i; j--) { System.out.print(" "); } for (int k = 1; k <= i; k++) { System.out.print("*"); } for (int m = 1; m < i; m++) { System.out.print("*"); } System.out.println(); } } }