用于循环三角形模式的Java

用于循环三角形模式的Java,java,loops,for-loop,Java,Loops,For Loop,嘿,我正在尝试创建一个模式,假设在这样的模式中输出5的倍数: 5 10 15 20 25 30 40 45 50 55 60 65 70 75 80 但我的输出是这样的 5 10 15 20 25

嘿,我正在尝试创建一个模式,假设在这样的模式中输出5的倍数:

5 10 15 20 25
  30 40 45 50
     55 60 65
        70 75
           80
但我的输出是这样的

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();
    }


    }
}
import java.util.Scanner;
公共班机
{
公共静态void main(字符串[]args){
int n=5;
int x=5;
对于(int i=n;i>=1;i--){
对于(int j=n-1;j>=i;j--){
系统输出打印(“”);
}

对于(int k=1;k您一次写入三个字符(空格、第一位数字、第二位数字),因此需要打印三个空格以获得正确的结果

基本上您的代码相同,但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();
}
用于(int i=n;i>=1;i--){
//这是相同的循环,但以不同的方式编写。
对于(int j=0;i对于(int k=1;k您一次写入三个字符(空格、第一位数字、第二位数字),因此需要打印三个空格以获得正确的结果

基本上您的代码相同,但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();
}
用于(int i=n;i>=1;i--){
//这是相同的循环,但以不同的方式编写。
对于(int j=0;i对于(int k=1;k我认为这个练习也是使用
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));
}

公共静态void main(字符串[]args){
int x=5;
int n=35;
int max=calculateTotalNumbers(n)*x;//计算最大数
int maxLenght=String.valueOf(max).length();//计算最大数字中的位数
对于(inti=n;i>=1;i--){
对于(intj=0;j对于(int k=1;k我认为这个练习也是使用
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));
}

公共静态void main(字符串[]args){
int x=5;
int n=35;
int max=calculateTotalNumbers(n)*x;//计算最大数
int maxLenght=String.valueOf(max).length();//计算最大数字中的位数
对于(inti=n;i>=1;i--){
对于(intj=0;jpublic class temp{
公共静态void main(字符串[]args){
int n=5;
int p=1;
对于(int i=0;i0){
curr+=“”;
}
}
对于(int j=i;j
公共类临时{
公共静态void main(字符串[]args){
int n=5;
int p=1;
对于(int i=0;i0){
curr+=“”;
}
}
对于(int j=i;j
实现您想要的最干净的方法是使用
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();
        }
    }
}
更新:

  5 10 15 20 25
    30 35 40 45
       50 55 60
          65 70
             75
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();
        }
    }
}

实现目标的最干净方法是使用
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();
        }
    }
}
更新:

  5 10 15 20 25
    30 35 40 45
       50 55 60
          65 70
             75
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();
        }
    }
}

我想你的输出丢失了35我想你的输出丢失了35
%4s
指定了一个长度为
4
的字符,在这个长度内,给定的字符串将被打印。
%4d
指定了一个长度为
4
的数字,在这个长度内,给定的整数将被打印。我建议你通过。
%4s
指定一个长度将在其中打印给定字符串的
4
个字符的h。
%4d
指定打印给定整数的
4
位长度。建议您仔细阅读。