![](/img/trans.png)
[英]How do I get my program to print out all of my input from a do while loop?
[英]How do I get my program to state an error and exit if input is < 1
因此,该程序仅打印基本的居中三角形。 用户输入他们希望三角形包含多少行。 我只是不确定如何插入错误消息并以此退出程序。 在不打扰任何事情的情况下最好的方法是什么?
导入java.util.Scanner;
公共课Triangle {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.print("Enter the number of lines: ");
Scanner input = new Scanner(System.in);
int numLines = input.nextInt();
for (int i = 0; i < numLines; i++)
{
for (int j = 0; j <= numLines - i; j++)
{
System.out.print(" "); //prints the proper number of spaces so that it's centered
}
for (int k = 0; k <= 2*i; k++)
{
System.out.print("*"); //prints proper number of *
}
System.out.println(); //makes new row
}
}
}
后
int numLines = input.nextInt();
您添加
if (numlines <= 0)
return;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.