Java basics---loop structure

Java basics-loop structure (two)-multiple loops

Preface: Using the single loop mentioned earlier can already solve many problems encountered, but why use multiple loops? Of course, if the problems encountered cannot be solved by a single cycle, then multiple cycles can be used.

[For example: how to use the loop structure described before to output the following graphics]
Insert picture description here
Maybe the first one can be done, but the second one is more laborious, and the third one just gives up! ! !

Therefore, in order to better solve this kind of bit problem, it is essential to learn multiple cycles.

What is multiple loop

Insert picture description here

Focus

Insert picture description here

As described above is the basic structure of multiple loops, of course, it can also be 3 layers, 4 layers and so on.However, it is recommended not to exceed 3 layers, Because more than 3 layers, the efficiency of program execution will be greatly reduced.

But what if we want to break out of the loop?

We talked about in the previous chapterbreak, In fact, there iscontinuewithreturn
Insert picture description here
Insert picture description here

Example of multiple loops

1. First, use multiple loops to do the first two graphics
//平行四边形
 for (int i = 0; i <5 ; i++) {						//控制行数
            for (int k = 0; k <4-i ; k++) {			//列-空格
                System.out.print(" ");
            }
            for (int j = 0; j < 5; j++) {			//列-*
                System.out.print("*");
            }
            System.out.println();
 }
//金字塔
System.out.println("请输入你要几行");          	  //输出数字金字塔
        int n = new Scanner(System.in).nextInt();
        for(int i =1;i<=n;i++){                   //外层循环控制行数,内层循环控制列
            for(int j=1;j<=n-i;j++){              //空格
                System.out.print(" ");
            }
            for(int k=1;k<=2*i-1;k++){            //输出内容
                System.out.print("*");			  //其实这里的 * 可以改成 i,你们可以试一下哦
            }
            System.out.print("\n");
  }
//圣诞树--其实就只在金子塔的基础上加了奇偶判断
System.out.println("请输入你要几行");          		  //圣诞树(上面的加入奇偶行判断)
        int n = new Scanner(System.in).nextInt();
        for(int i =1;i<=n;i++){                       //外层循环控制行数
            for(int j=1;j<=n-i;j++){                  //空格
                System.out.print(" ");
            }
            if(i%2==0){
                for(int k=1;k<=2*i-1;k++){            //输出内容
                    System.out.print("~");
                }
            }else {
                for(int k=1;k<=2*i-1;k++){            //输出内容
                    System.out.print("*");
                }
            }
            System.out.print("\n");
  }

The result is shown in the figure
Insert picture description here

Second, let's do a little bit about the classic problem of multiple cycles

1. Prime number

//输出2-100之间的素数
for (int i = 2; i <=100 ; i++) {			//外层循环2-100
            int tag = 0;					//标记是否为素数
            for (int j = 2; j <i ; j++) {	//内层循环从2到自生
                if(i%j==0){					//判断是否为素数
                    break;
                }else {
                    tag = 1;
                }
            }
            if(tag==1){
                System.out.print(i+"\t");
         	 }
 }

Insert picture description here
[Hint: The output format here is a bit ugly, don't worry, we can output according to how many lines per line after we finish the next chapter!
2. 9*9 multiplication table

//9*9乘法表
for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(i+"*"+j+"="+i*j+"\t");
            }
            System.out.println();
 }

Insert picture description here

9*9 multiplication table in another format

for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= 9-i; j++) {
                System.out.print("\t\t");		//为了美观这里格式不要出
            }
            for (int j = 1; j <= i; j++) {
                System.out.print(i+"*"+j+"="+i*j+"\t");
            }
            System.out.println();
 }

Insert picture description here
You can see the difference between the two.

3. Number of palindrome

//回文数
System.out.println("请输出一个数,判断它是否为回文数");
        int x = new Scanner(System.in).nextInt();
        if (x < 0) {
            System.out.println(false);
        }
        int reverse = 0;
        int number = x;
        while (number != 0) {
            int temp = number % 10;		//取最后一个数
            number /= 10;				//去掉最后一位
            reverse = reverse * 10;		//低位变高位
            reverse += temp;			//形成相反的数
        }
System.out.println(reverse==x);			//比较反转前后,相同则是回文数

The result is shown in the figure
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43288259/article/details/112499460