package com.karl; import com.sun.net.httpserver.SimpleFileServer; public class forDemo { //应用场景 //减少代码的重复编写,灵活的控制程序的执行 //打印多行HelloWord public static void main(String[] args) { for (int i = 0;i<5;i++){ System.out.println("王大锤"); } System.out.println("___________________________________"); for (int i=1;i<=10;i+=2){ //1 3 5 7 9 5次 System.out.println("王大锤1"); } System.out.println("___________________________________"); for (int i=1;i<=10;i*=2){ //1 2 4 8 4次 System.out.println("王大锤+6"); } System.out.println("___________________________________"); for (int i=1;i<=100;i++){ System.out.println(i); } System.out.println("___________________________________"); //求和循环 //定义一个变量 int sum= 0; //定义循环 for (int i = 1; i< 5; i++) { //1 2 3 4 5 sum+=i; //sum=sum+1; } System.out.println("和"+sum); System.out.println("___________________________________"); //求奇数和 int sum1 =0; for (int i = 1; i < 100; i+=2) { //1 3 5 7 .... sum1 += i; } System.out.println("奇数和:"+sum1); System.out.println("___________________________________"); int sum2=0; for (int i = 1; i < 100; i++) { //1 2 3 4 5 6 7 ..... 99 100 //使用if分支,判断i此时记住的数据是否为奇数,是奇数我们才累加给一个变量 if(i % 2 == 1){ //1 3 5 7 9..99 sum2+=i; } System.out.println("奇数和:"+sum2); } } }
标签:场景,int,循环,应用,com,public From: https://www.cnblogs.com/Karl-hut/p/17437991.html