由于某些原因,只有奇数行才能正确分隔。有人可以解释这个吗?这是我的代码。在java工作中需要帮助制作等边三角形的代码
import java.util.Scanner;
public class Triangle {
public static void main(String args[]) {
int i, j, k1;
Scanner in = new Scanner(System.in);
System.out.println("Enter the length of the side of your equilateral triangle: ");
int side = in.nextInt();
System.out.println("Enter the character you want your triangle filled with: ");
char fill = in.next().charAt(0);
while (side<=0 || side>50){
System.out.print("Please enter a length between 1 and 50: ");
side = in.nextInt();
break;
}
for(i=1; i<=side;i++){
for (k1=0; k1 < (side-i/2);k1++){
System.out.print("\t");
}
for(j=1;j<=i;j++){
System.out.print(fill + "\t");
}
System.out.println('\n');
}
in.close();
}
}
这是我的结果: 输入您的等边三角形的边长:输入你希望你的三角形填充字符: *
*
* *
* * *
* * * *
* * * * *
我想这一点:
#
# #
# # #
# # # #
# # # # #
你在做什么,你期望什么? – Andreas
无关:带有无条件'break'的while循环有什么意义? – Andreas
这是我的第一个java类,所以我认为在用户输入正确的数字后,休息会继续我的代码的其余部分 –