【发布时间】: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
【问题讨论】:
-
Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. 见:How to create a Minimal, Complete, and Verifiable example。
-
欢迎来到 SO。请设身处地为可能回答您问题的人着想。正方形和三角形?至少向我们展示输出应该是什么样子
-
“打印一个完整的金字塔” - 我可以想象有很多方法可以“打印一个金字塔”,只需输入您想要显示的 asciis,一切都会 100% 清晰
-
您谈论的是 3D 对象(金字塔)和“打印”。我假设“打印金字塔”是指“制作一些控制台输出”,即 2D ascii 图像。这至少涉及投影和离散化。你的解释不是珍贵的。正如@LonelyNeuron 所说,请至少显示到目前为止您得到的输出以及完整输出的样子。
标签: java loops nested-loops