Java中使用for循环的等边三角形
我试图画一个由星号字符组成的等边三角形,但当用户输入行号时,该行将被画成x,整个三角形将是*但空格中有一个错误 这是我的密码:Java中使用for循环的等边三角形,java,console,Java,Console,我试图画一个由星号字符组成的等边三角形,但当用户输入行号时,该行将被画成x,整个三角形将是*但空格中有一个错误 这是我的密码: int number_of_stars = getHeight(); for (int rows=1; rows <= getHeight(); rows++) { for (int spaces=1; spaces <= number_of_stars; spaces++) { Sy
int number_of_stars = getHeight();
for (int rows=1; rows <= getHeight(); rows++)
{
for (int spaces=1; spaces <= number_of_stars; spaces++)
{
System.out.print(" ");
}
if(rows == getRowNum()){
for (int star=1; star <= rows; star++)
{
System.out.print("x");
System.out.print(" ");
}
System.out.println("");
rows = getRowNum()+1;
System.out.print(" ");
System.out.print(" ");
System.out.print(" ");
}
for (int star=1; star <= rows; star++)
{
System.out.print("*");
System.out.print(" ");
}
System.out.println("");
number_of_stars = number_of_stars - 1;
}
第九行和第十行不正确我相信您想要一个简单的if-else,当您添加一个额外的循环来打印xs时,它取消了对齐。我想你只是想
public static void main(String[] args) {
int number_of_stars = getHeight();
for (int rows = 1; rows <= getHeight(); rows++) {
for (int spaces = 1; spaces <= number_of_stars; spaces++) {
System.out.print(" "); // <-- indent(s)
}
for (int star = 1; star <= rows; star++) {
if (rows == getRowNum()) {
System.out.print("x"); // <-- one row is "x"
} else {
System.out.print("*"); // <-- others are "*"
}
System.out.print(" ");
}
System.out.println("");
number_of_stars = number_of_stars - 1;
}
}
对于特定的行号,您要做的唯一更改是打印x而不是*并且您可以简单地将字符更改为打印。我尝试了以下示例代码:
public static void main(String[] args) {
int userRowNumber = 5;
int height = 10;
int number_of_stars = height;
String charToPrint;
for (int rows=1; rows <= height; rows++)
{
charToPrint = "*";
if(rows == userRowNumber){
charToPrint = "x";
}
for (int spaces=1; spaces <= number_of_stars; spaces++)
{
System.out.print(" ");
}
for (int star=1; star <= rows; star++)
{
System.out.print(charToPrint);
System.out.print(" ");
}
System.out.println("");
number_of_stars = number_of_stars - 1;
}
}
打印出预期的内容。对于代码中的问题,我建议您自己调试并找出答案。现在是启动调试器并逐步执行的正确时机。
public static void main(String[] args) {
int number_of_stars = getHeight();
for (int rows = 1; rows <= getHeight(); rows++) {
for (int spaces = 1; spaces <= number_of_stars; spaces++) {
System.out.print(" "); // <-- indent(s)
}
for (int star = 1; star <= rows; star++) {
if (rows == getRowNum()) {
System.out.print("x"); // <-- one row is "x"
} else {
System.out.print("*"); // <-- others are "*"
}
System.out.print(" ");
}
System.out.println("");
number_of_stars = number_of_stars - 1;
}
}
public static void main(String[] args) {
int userRowNumber = 5;
int height = 10;
int number_of_stars = height;
String charToPrint;
for (int rows=1; rows <= height; rows++)
{
charToPrint = "*";
if(rows == userRowNumber){
charToPrint = "x";
}
for (int spaces=1; spaces <= number_of_stars; spaces++)
{
System.out.print(" ");
}
for (int star=1; star <= rows; star++)
{
System.out.print(charToPrint);
System.out.print(" ");
}
System.out.println("");
number_of_stars = number_of_stars - 1;
}
}