Tou Ge (educoder) Chapter 2 Introduction to Java - Control Structure Introduction to Java - Basics of Loop Structure

Table of contents

Level 1: Java loop structure while loop

Level 2 Java loop structure while loop exercise

Level 3 Java loop structure do...while loop

Level 4 while, do...while loop test questions Edit

 Level 5 break and continue keywords

Level 6 break and continue keyword test questionsEdit

Level 7 Java loop structure for loop

 Level 8 for loop test questionsEdit


Level 1: Java loop structure while loop

package step1;

public class HelloWorld {
	public static void main(String[] args) {
		
		/*****start*****/
		int i=1;
		while(i<=6){
			System.out.println("做了"+i+"个俯卧撑");
			i++;
		}
		
		
		/*****end*****/
		
		
		
		
	}
}

Level 2 Java loop structure while loop exercise

package step2;

public class HelloWorld {
	public static void main(String[] args) {
		
		
		/*****start*****/
		int i=1,n=0;
		while(i<=100){
			n+=i;
			i++;
		}
		System.out.println("1到100相加的结果为"+n);
		
		
		/*****end*****/
		
		
		
	}
}

Level 3 Java loop structure do...while loop

package step3;

public class HelloWorld {
	public static void main(String[] args) {
		int count= 0;	//定义变量存储6的倍数出现的次数
		/*****start*****/
		int i=0;
		do{
			i++;
			if(i%6==0){
				count++;
			}
		}while(i<=100);
		

		
		/*****end*****/
		System.out.println("6的倍数出现的次数为:" + count);
	}
}

Level 4 while, do...while loop test questions

 

 

 Level 5 break and continue keywords

package step4;

public class HelloWorld {
	public static void main(String[] args) {
		
		int i = 0;
		
		while(i <= 20){
			i++;
			/*****start*****/
			if(  i%2==0    ){
                System.out.println( i + "是偶数");
			
            
			}else
			System.out.println(i + "是奇数");
			
            if( i==13    ) {

			 break;
            }
			
			/*****end*****/
		}
		
	}
}

Level 6 break and continue keyword test questions

Level 7 Java loop structure for loop

package step5;

import java.util.Scanner;

public class HelloWorld {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请给定一个自然数N:");
		int N = sc.nextInt();//获取输入的整数N
		int sum = 1;		
		/*****start*****/
		for(int i=1;i<=N;i++){
			sum*=i;
		}
		
		
		
		
		
		
		/*****end*****/
		
		System.out.println("自然数N的阶乘为" + sum);
	}
}

 Level 8 for loop test questions

 

Guess you like

Origin blog.csdn.net/qq_35353972/article/details/126926445