EduCoder Java Programming---Introduction to Java (Chapter 5) - Basic Answers to Loop Structures

Level 1: while loop of Java loop structure

package step1;

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

Level 2: While loop exercise of Java loop structure

package step2;

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




		/**********End**********/
		
		
		
	}
}

Level 3: do...while loop of Java loop structure

package step3;

public class HelloWorld {
	public static void main(String[] args) {
		int count= 0;	//定义变量存储6的倍数出现的次数
		/*****start*****/
		int i = 1;
		do{
			if(i%6==0){
				count= count+1;
			}
			i++;
		}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 <= 13){
			i++;
			/*****start*****/
			if(i%2==0&&i<=13){
                System.out.println(i+"是偶数");
				i++;
			}
            if(i%2!=0&&i<=13 ) {
				System.out.println(i+"是奇数");
        
            }
			
			/*****end*****/
		}
		
	}
}

Level 6: break and continue keyword test questions

Level 7: The for loop of the Java loop structure

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 = N;i>0;i--){
			sum =sum*i;
		}
		
		/*****end*****/
		
		System.out.println("自然数N的阶乘为" + sum);
	}
}

Level 8: for loop test questions

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324156661&siteId=291194637