Java-for应用

时间:2020-11-21
本文章向大家介绍Java-for应用,主要包括Java-for应用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

For 循环

  • for循环语句是支持迭代的一种通用结构,是最有效,最灵活的循环结构。

  • for 结构

    for (int i = 0; i < 100; i++) {
    //循环内容
    }

练习

package com.chenhao.struct;
import javax.swing.plaf.synth.SynthOptionPaneUI;
public class Demo05 {
   public static void main(String[] args) {
       //练习1:计算0-100之间的奇数和偶数的和
       int even = 0;//偶数
       int odd = 0;//奇数
       for (int i = 0; i <= 100; i++) {
           if(i%2==0){
               even += i;
          }else{
               odd += i;
          }
      }
       System.out.println("奇数的和是:"+odd+",偶数的和是:"+even);
       System.out.println("总和是:"+(odd+even));
       System.out.println("-----------------------------------------");
       //练习2:用while 或 for 循环输出 1-300之间能被5整除的数,并且每行输出3个
       for (int i = 0; i < 30; i++) {
           if (i%5==0){
               System.out.print(i+"\t");
          }
           if (i%15==0){
               System.out.print("\n");
          }
      }
       System.out.println();
       System.out.println("-----------------------------------------");
       //练习3:打印99乘法表
       for (int i = 1; i <= 9; i++) {
           for(int j = 1;j <= i; j++){
               System.out.print(j+"X"+i+"="+i*j+"\t");
          }
           System.out.println();
      }
  }
}
//println 输出完会换行
//print 输出完不会换行

 

原文地址:https://www.cnblogs.com/chncc123/p/14017533.html