Comprehensive example one

Example 1: (Read in an indeterminate integer from the keyboard, and judge the number of positive and negative numbers read, stop when you enter 0)

import java.util.Scanner;
class Test1{
    
    
	public static void main(String[] args){
    
    

		Scanner scan = new Scanner(System.in);

		int positiveNumber = 0;//记录正数的个数
		int negativeNumber = 0;//记录负数的个数

		while(true){
    
    //for(;;)
			System.out.println("请输入一个整数:");
			int number = scan.nextInt();

			//判断number的正负情况
			if(number > 0){
    
    
				positiveNumber++;
			}else if(number < 0){
    
    
				negativeNumber++;
			}else{
    
    
				//一旦执行break,跳出循环
				break;
			}
		}
		System.out.println("正数个数为:"+ positiveNumber);
		System.out.println("负数个数为:"+ negativeNumber);

	}
}

to sum up:

1. The structure that does not limit the number of times in the loop condition part: for (;;) or while (true)
2. The way to end the loop:
①: return false in the loop condition part
②: execute break in the loop body;

Example 2: (Nine-Nine Multiplication Table)

class Test2 {
    
    
	public static void main(String[] args){
    
    

		for(int i = 1;i <= 9;i++){
    
    
			for(int j = 1;j <= i;j++){
    
    
				System.out.print(i + "*" + j + "=" + i*j + " ");
			}
			System.out.println();
		}
	}
}

Insert picture description here

Example 3: (traverse all prime numbers within 100)

method one:

class Test3 {
    
    
	public static void main(String[] args) {
    
    

		for(int i = 2;i <= 100;i++){
    
    
		
		boolean isFlag = true;//标识i是否被j除尽过

			for(int j = 2;j < i;j++){
    
    

				if(i % j == 0){
    
    
					isFlag = false;
				}
			}
			if(isFlag == true){
    
    
				System.out.println(i);
			}
		}
	}
}

Way two:

class Test3 {
    
    
	public static void main(String[] args) {
    
    

		boolean isFlag = true;//标识i是否被j除尽过

		for(int i = 2;i <= 100;i++){
    
    

			for(int j = 2;j < i;j++){
    
    

				if(i % j == 0){
    
    
					isFlag = false;
				}
			}
			if(isFlag == true){
    
    
				System.out.println(i);
			}
			//重置isFlag
			isFlag = true;
		}
	}
}

Method three: (optimization one) // unoptimized: 25886ms optimized: 5327ms

class Test3 {
    
    
	public static void main(String[] args) {
    
    

        boolean isFlag = true;//标识i是否被j除尽过

		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long start = System.currentTimeMillis();

		for(int i = 2;i <= 100000;i++){
    
    

			for(int j = 2;j < i;j++){
    
    

				if(i % j == 0){
    
    
					isFlag = false;
					break;//优化一,只对本身非质数的数有效
				}
			}
			if(isFlag == true){
    
    
				System.out.println(i);
			}
			//重置isFlag
			isFlag = true;
		}
		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long end= System.currentTimeMillis();

		System.out.println("所花费的时间为:" + (end - start));
		//未优化:25886ms  优化:5327ms
	}
}

Method four: (optimization two): optimization one: 5327ms optimization two: 1998ms

class Test3 {
    
    
	public static void main(String[] args) {
    
    

        boolean isFlag = true;//标识i是否被j除尽过

		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long start = System.currentTimeMillis();

		for(int i = 2;i <= 100000;i++){
    
    

			//只需到根号i,对本身时质数的数有效
			for(int j = 2;j <= Math.sqrt(i);j++){
    
    

				if(i % j == 0){
    
    
					isFlag = false;
					break;//优化一,只对本身非质数的数有效
				}
			}
			if(isFlag == true){
    
    
				System.out.println(i);
			}
			//重置isFlag
			isFlag = true;
		}
		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long end= System.currentTimeMillis();

		System.out.println("所花费的时间为:" + (end - start));
		//优化一:5327ms  优化二:1998ms
	}
}

Method five: (optimization three) // optimization 2: 1998ms optimization 3: 21ms

class Test3
public static void main(String[] args) {
    
    

        boolean isFlag = true;//标识i是否被j除尽过
		int count = 0;//记录质数的个数

		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long start = System.currentTimeMillis();

		for(int i = 2;i <= 100000;i++){
    
    

			//只需到根号i,对本身时质数的数有效
			for(int j = 2;j <= Math.sqrt(i);j++){
    
    

				if(i % j == 0){
    
    
					isFlag = false;
					break;//优化一,只对本身非质数的数有效
				}
			}
			if(isFlag == true){
    
    
				//System.out.println(i);
				count++;
			}
			//重置isFlag
			isFlag = true;
		}
		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long end= System.currentTimeMillis();

		System.out.println("质数的个数为:" + count);
		System.out.println("所花费的时间为:" + (end - start));
	}
}

Method 6: Labeled break and continue

class Test3 
public static void main(String[] args) {
    
    

		int count = 0;//记录质数的个数

		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long start = System.currentTimeMillis();

		lable:for(int i = 2;i <= 100000;i++){
    
    

			//只需到根号i,对本身时质数的数有效
			for(int j = 2;j <= Math.sqrt(i);j++){
    
    

				if(i % j == 0){
    
    
					continue lable;
				}
			}
			//能执行到此步骤的都是质数
				count++;
			}
			//重置isFlag
			isFlag = true;
		}
		//获取当前时间距离1970-01-01 00:00:00 的毫秒数
		long end= System.currentTimeMillis();

		System.out.println("质数的个数为:" + count);
		System.out.println("所花费的时间为:" + (end - start));
	}
}

Insert picture description here

Guess you like

Origin blog.csdn.net/xue_yun_xiang/article/details/110090597