packagecom.lv.struct;publicclassForDemo01{publicstaticvoidmain(String[] args){int a =1;//初始化条件while(a <=100){//条件判断System.out.println(a);//循环体
a +=2;//迭代}System.out.println("while循环结束");//For循环:最有效最灵活的循环结构for(int i =1; i <=100; i++){//初始化值,条件判断。迭代System.out.println(i);}System.out.println("for循环结束");for(;;){//死循环}}}
packagecom.lv.struct;publicclassForDemo02{publicstaticvoidmain(String[] args){//练习1:计算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);}}
packagecom.lv.struct;publicclassForDemo03{publicstaticvoidmain(String[] args){//练习2:用whlie或者for循环输出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 输出完不会换行}}
packagecom.lv.struct;publicclassForDemo05{publicstaticvoidmain(String[] args){int[] numbers ={10,20,30,40,50};//定义了一个数组//遍历数组的元素for(int i =0; i <5; i++){System.out.println(numbers[i]);}System.out.println("===========================================");//增强for循环:遍历数字的元素for(int x : numbers){System.out.println(x);//简化}}
If
packagecom.lv.struct;//if单选择结构importjava.util.Scanner;publicclassIfDemo01{publicstaticvoidmain(String[] args){Scanner scanner =newScanner(System.in);System.out.println("请输入内容");String s = scanner.nextLine();//equals:判断字符串是否相等if(s.equals("hello")){System.out.println(s);//如果成立,输出s}System.out.println("End");
scanner.close();}}