,因此此程序只需打印一个基本的中心三角形即可。用户输入他们希望三角形有多少行。我只是不确定如何插入错误消息并在此中退出程序。在不破坏任何事情的情况下,最好的方法是什么?
导入java.util.scanner;
公共类三角{
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;