gpt4 book ai didi

java - Java 中 for 循环的问题

转载 作者:搜寻专家 更新时间:2023-10-31 20:03:46 25 4
gpt4 key购买 nike

因为我真的很喜欢编程,而且我喜欢在空闲时间编程,所以我试图创建一个输出看起来像 x 的代码。像这样的东西。

x    x
x x
x
x x
x x

所以我希望用户输入“x”的高度。这是我到目前为止的代码,我真的不知道如何继续。我只需要一个提示,或者是否有人可以告诉我哪里出错了。

import java.util.Scanner;    
public class x{
public static void main(String[] args){
Scanner kbd = new Scanner(System.in);
int height;
System.out.print("Enter the height of the X: " );
height = kbd.nextInt();
for (int i = 1; i <= height; i++){
for (int j = 1; j <= height; j++) {
if(i ==j || j+i == height + 1)
System.out.println("x");
else
System.out.print(" ");
}
}
}
}

最佳答案

两个变化:

  • System.out.println("x"); 更改为 System.out.print("x");(打印后删除 ln)

  • 两行之后

        System.out.print(" ");
    }

    添加

    System.out.println();

关于java - Java 中 for 循环的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16241884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com