java打印金字塔嵌套循环的另一面

时间:2017-10-30 09:20:02

标签: java loops nested-loops

我目前只是在创建金字塔的另一面。我希望我的程序向用户询问5到15之间的数字。使用该数字打印出正方形和三角形。在我到达金字塔之前,我已经能够完成所有工作。我可以创建金字塔的一面,但我注意到在创建另一面时我忽略了一些东西。任何关于让我走向正确方向的指导都将不胜感激。

import java.util.Scanner;
public class doLoop {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Scanner input = new Scanner(System.in);

        int number;
        final int minimum = 5;
        final int maximum = 15;

        do {
            System.out.print("Enter a number between" + " " + minimum + " " + "and" + " " + maximum + ":" );
            number = input.nextInt();

            for(int i = 1; i <= number; i++) {
                for(int j = 1; j <= number; j++) {
                    System.out.print(j + "  ");
                }
                System.out.println();
            }
            for(int column = 1; column <= number; column++) {
                for(int row = 1; row <= number ; row++) {
                    if(column >= row) { 
                        System.out.print(row);
                    } else {
                        System.out.print(" ");
                    }
                }

                System.out.println(" ");
            }

            if (number <= minimum || number >= 15) 
                System.out.println("Sorry, invalid");
        } while (number <= minimum || number >= maximum); 
    }
}


**Here is my current output:**

Enter a number between 5 and 15:5
1  2  3  4  5  
1  2  3  4  5  
1  2  3  4  5  
1  2  3  4  5  
1  2  3  4  5  
1     
12    
123   
1234  
12345 
Sorry, invalid
Enter a number between 5 and 15:

**This is what i'm working towards:**

Enter a number between 5 and 15: 2
Sorry, 2 is invalid.  Please try again.
Enter a number between 5 and 15: 20
Sorry, 20 is invalid.  Please try again.
Enter a number between 5 and 15: 10

1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10
1  2  3  4  5  6  7  8  9 10


                            1
                         2  1  2
                      3  2  1  2  3
                   4  3  2  1  2  3  4
                5  4  3  2  1  2  3  4  5
             6  5  4  3  2  1  2  3  4  5  6
          7  6  5  4  3  2  1  2  3  4  5  6  7
       8  7  6  5  4  3  2  1  2  3  4  5  6  7  8
    9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9
10  9  8  7  6  5  4  3  2  1  2  3  4  5  6  7  8  9 10

2 个答案:

答案 0 :(得分:0)

你的代码有多个错误!!!

  1. 您的代码将永远循环
  2. 您不需要do loop
  3. 如果我理解你的问题是你想要金字塔的左侧。您可以通过从负用户输入循环到用户插入的值来实现它
  4. 这是一个剪切代码,你需要根据你的需要进行一些调整!!!

    import java.util.Scanner;
    
    public class doLoop {
    
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            int number;
            final int minimum = 5;
            final int maximum = 15;
    
            System.out.print("Enter a number between" + " " + 
                   minimum + " " + "and" + " " + maximum + ":");
            number = input.nextInt();
    
            if (number <= minimum || number >= 15) {
                System.out.println("Sorry, invalid");
                return;
            }
    
            for (int i = 1; i <= number; i++) {
                for (int j = 1; j <= number; j++) {
                    System.out.print(j + "  ");
                }
                System.out.println();
            }
    
            for (int row = 1; row < number; row++) {
    
                for (int column = -(number - 1); column <= number; column++) {
                    int absValue = Math.abs(column);
                    // you need to use the absolute value 
                    // to print the positive value and to perform column checks
                    if (absValue <= row)
                        System.out.print(absValue);
                    else {
                    // if the absolute value is greater the the current print 1 or 2 spaces 
                    // based on the value of the column 
                    //(2 spaces if lower then 10 otherwise 1 space only)
                        System.out.print(absValue < 10 ? " " : "  ");
                    }
                    // If the absolute value of column is -1 or 1 you need to change 
                    // the value to "1" to bypass the printing of 101
                    if (absValue == 1)
                    {
                        column = 1;
                    }
                }
                System.out.println();
            }
        }
    
    }
    

答案 1 :(得分:0)

我宁愿编辑我的工作,而不是仅仅复制你所做的事情。另外,我不了解你的所有代码。我能够完成我想要的三角形。但由于某种原因,我不能让我的循环遵循我设定的条件。在我为形状添加for循环之前,它最初工作,但现在我仍然坚持如何让它再次跟随它们。有什么建议吗?

import java.util.Scanner;
public class doLoop {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);

    int number;
    final int minimum = 5;
    final int maximum = 15;




    do {
        System.out.print("Enter a number between" + " " + minimum + " " + "and" + " " + maximum + ":" );
         number = input.nextInt();

    if (number <= minimum || number >= maximum) {
        System.out.println("Sorry, invalid");
   break;
}
                for(int i = 1; i <= number; i++) {

        for(int j = 1; j <= number; j++) {
            System.out.print(j + "  ");

        }
        System.out.println();

        }
                int columns = 1;

                for (int i = number; i >= 1; i--)   
                {
                     for (int j = 1; j <= i*2; j++)
                     {
                         System.out.print(" ");

                }
                for (int j = i; j <= number; j++) 
                {
                    System.out.print(j + " ");
                }

                for (int j = number - 1; j >= i; j--)
                {
                    System.out.printf(j + " ");
                }

                    System.out.println();

                    columns++;
                }





    } while (number <= minimum || number >= maximum); 




}


}