繁体   English   中英

Java For Loop 三角形图案

[英]Java For Loop triangle pattern

嘿,我正在尝试创建一个假设为 output 5 的倍数的模式,如下所示:

5 10 15 20 25
  30 40 45 50
     55 60 65
        70 75
           80

但我的 output 是这样的

5 10 15 20 25                                                                                                                                 
 30 35 40 45                                                                                                                                  
  50 55 60                                                                                                                                    
   65 70                                                                                                                                      
    75 

但是当我在印刷品中加上星号时:

*****                                                                                                                                         
 ****                                                                                                                                         
  ***                                                                                                                                         
   **                                                                                                                                         
    *

这是我的代码:

import java.util.Scanner;

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

    int n = 5;
    int x =5;

    for(int i = n; i>=1; i--){

        for(int j = n-1; j>=i; j--){
            System.out.print(" ");
        }
        for(int k = 1; k<=i; k++){
            System.out.print(x+" ");
            x+=5;

        }

        System.out.println();
    }


    }
}

有人可以帮助我吗? 我花了将近 3 个小时试图弄清楚这一点。 任何帮助,将不胜感激。 谢谢!

您一次写入三个字符(空格、第一个数字、第二个数字),因此您需要打印三个空格才能获得正确的结果。

基本上您的代码相同,但 System.out.print 中有三个空格:

for(int i = n; i>=1; i--){

    //This is the same loop, but written in a different way.
    for(int j = 0; i < n-i; j++){
        System.out.print("   "); //THREE SPACES HERE
    }
    for(int k = 1; k<=i; k++){
        System.out.print(x+" ");
        x+=5;

    }

    System.out.println();
}

现在,您还要在开头打印 5,它是一个数字长,因此您之前需要另一个空格。 所以你需要一个条件结构来做到这一点:

//This is the same loop, but written in a different way.
for(int j = 0; i < n-i; j++){
    if(j == 0 && x <= 5) //First cycle (remember 5, which is 1 digit)
        System.out.print("  "); //TWO SPACES HERE
    else
        System.out.print("   "); //THREE SPACES HERE
}

请注意,这仅适用于 2 位数字,因此如果您还要使用 3 位或更多位数字,则需要执行与我们使用数字 5 类似的操作。

实现您想要的最干净的方法是使用printf ,如下所示:

public class Main {
    public static void main(String[] args) {
        int n = 5;
        int x = 5;

        for (int i = n; i >= 1; i--) {
            for(int j=1;j<n-i+1;j++)
                System.out.printf("%3s"," ");
            for (int k = 1; k <= i; k++) {
                System.out.printf("%3d",x);
                x += 5;
            }
            System.out.println();
        }
    }
}

Output:

  5 10 15 20 25
    30 35 40 45
       50 55 60
          65 70
             75

更新:

如果您对printf ,可以使用以下解决方案:

public class Main {
    public static void main(String[] args) {
        int n = 5;
        int x = 5;
        String space=" ";
        for (int i = n; i >= 1; i--) {
            for(int j=1;j<(n-i)*3;j++)
                System.out.print(space);
            for (int k = 1; k <= i; k++) {
                System.out.print(x+space);
                x += 5;
            }
            System.out.println();
        }
    }
}

Output:

5 10 15 20 25 
  30 35 40 45 
     50 55 60 
        65 70 
           75 

我认为这个练习也是练习System.out.printf()的好时机

这是我将如何做到这一点:



public static void main(String[] args) {                   
    int x = 5;                                             
    int n = 35;                                            
    int max = calculateTotalNumbers(n) * x;  // calculate the highest number              
    int maxLenght = String.valueOf(max).length(); //calculate the numbers of digits in the highest number         

    for(int i = n; i>=1; i--){                             
        for (int j = 0; j < n - i; j++) {      
            //makes sure you will have the same length spaces everywhere            
            System.out.printf("%1$"+maxLenght+"s ", "");   
        }                                                  
        for (int k = 1; k <= i; k++) { 
            //this formatting will ensure every digit has the same length with padding included. Check out the javadoc of System.out.printf();                     
            System.out.printf("%" + maxLenght + "d ", x);  
            x += 5;                                        

        }                                                  
        System.out.println();                              
    }                                                      
}                                                          

// this method will calculate the total number of numbers you will print in the triangle
private static int calculateTotalNumbers(int n) {
    if (n <= 0) return 0;
    else return (n + calculateTotalNumbers(n -1));
}

只是不要调用 println,并去掉多余的空格

for(int i = n; i>=1; i--){

//you don't need this
/*
    for(int j = n-1; j>=i; j--){

        System.out.print(" ");
    }
*/ 
    for(int k = 1; k<=i; k++){
        System.out.print(x+" ");
        x+=5;

    }

}
//move this here
System.out.println();
public class temp {
    public static void main(String[] args) {
        int n = 5;
        int p = 1;
        for (int i = 0; i < n ; i ++){
            String curr = "";
            for (int j = 0 ; j < n - (n - i); j ++){
                curr += "  ";
                if (j > 0) {
                    curr += " ";
                }
            }
            for (int j = i; j < n; j ++){
                curr += Integer.toString(n * p);
                curr += " ";
                p += 1;
            }
            System.out.println(curr);
        }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM