【问题标题】:java print other side of pyramid nested loopjava打印金字塔嵌套循环的另一面
【发布时间】:2017-10-30 09:20:02
【问题描述】:

我目前正忙于创建金字塔的另一面。我想让我的程序向用户询问 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

【问题讨论】:

标签: java loops nested-loops


【解决方案1】:

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

  1. 您的代码将永远循环
  2. 你不需要do loop
  3. 如果我理解正确,您的问题是您想要金字塔的左侧。您可以通过从负用户输入循环到用户插入的值来实现它

这是一个截断的代码,您需要根据自己的需要进行一些调整!!!

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

}

【讨论】:

    【解决方案2】:

    我宁愿编辑我所做的工作,而不是仅仅复制你所做的工作。另外,我不了解您的所有代码。我能够完成我正在寻找的三角形。但由于某种原因,我无法让我的循环遵循我设置的条件。在我为形状添加 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); 
    
    
    
    
    }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2015-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多