我正在尝试使用方法来创建一个java程序,以便根据3到10之间的用户条目构建一个特定大小的沙漏形图。然而,当我运行程序时,我得到一个无限循环。 / p>
public static void main(String[] args){
int height;
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter an integer between 3 and 10 to control the" +
" heigth of the hour glass: ");
height = keyboard.nextInt();
line();
topHalf();
bottomHalf();
line();
}
public static void line() {
System.out.println("+");
for (int i = 1; 1<= 10; i++){
System.out.println("-");
System.out.println("+");
}
}
public static void topHalf()
{
for(int line = 1; line <= 3; line++){
System.out.println("|");
for (int i = 1; i<= (line - 1); i++){
System.out.println(" ");
}
System.out.println("\\");
for (int i = 1; i <= (6 - 2 * line); i++){
System.out.println(".");
}
System.out.print("/");
for (int i = 1; i <= (line - 1); i++){
System.out.println(" ");
}
System.out.println("|");
}
}
public static void bottomHalf()
{
for (int line = 1; line <= 3; line++){
System.out.println("|");
for (int i = 1; i <= (3 - line); i++){
System.out.println(" ");
}
System.out.println("/");
for (int i = 1; i <= 2 * (line - 1); i++){
System.out.println(".");
}
System.out.println("\\");
for (int i = 1; i <= (3 - line); i++){
System.out.print(" ");
}
}
}
}
我真的不知道发生了什么,但我确定它不是沙漏形状。当我运行它时,唯一发生的事情是+的无限循环然后 - 。非常感谢任何帮助,谢谢!
答案 0 :(得分:4)
您的1
循环条件为i
代替for
- 1 <= 10
始终为真,因此无限循环。你想改变:
for (int i = 1; 1<= 10; i++){
到此:
for (int i = 1; i<= 10; i++){