一、while循环
public class WhileDemo01 { public static void main(String[] args) { //输出1~100,并且求和 int i = 1; int sum = 0; while(i != 101){ System.out.println(i); sum=sum+i; i++; } System.out.println(sum); } }
二、DoWhile循环
public class WhileDemo01 { public static void main(String[] args) { //输出1~100,并且求和 int i = 1; int sum = 0; do{ System.out.println(i); sum=sum+i; ++i; }while(i<=100); System.out.println(sum); } }
public class WhileDemo02 { public static void main(String[] args) { int a = 0; while(a<0){ System.out.println(a); a++; } System.out.println("======================"); do{ System.out.println(a); a++; }while(a<0); } }//输出0
三、for循环
public class ForDemo01 { public static void main(String[] args) { //练习一:计算0-100之间奇数和偶数的和 int oddsum = 0; int evensum = 0; for(int i = 0;i <100;i++){ if(i % 2 == 1){ oddsum = oddsum + i; }else{ evensum = evensum + i; } } System.out.println("奇数和:"+oddsum+"偶数和:"+evensum); } }
public class ForDemo02 { public static void main(String[] args) { //练习二:用while或者for循环输出1-1000之间能被5整除的数,并且每行输出3个。 for(int i = 0;i <= 1000;i++){ if(i % 5 == 0){ System.out.print(i+"\t");//把println换成print,println输出完会换行,print输出完不会换行 } if(i % (5 * 3) == 0){ System.out.println("\n");//也可以是System.out.println(); } } } }
public class ForDemo03 { public static void main(String[] args) { //打印九九乘法表 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(); } } }
标签:Java,String,int,sum,While,static,06,main,public From: https://www.cnblogs.com/sankouyitouju/p/17833605.html