gpt4 book ai didi

java - Java中的方形显示

转载 作者:行者123 更新时间:2023-12-01 21:45:58 26 4
gpt4 key购买 nike

我有关于循环的作业,但我卡住了。要求的输出应该是:

***
***
***

当你输入3

我的代码输出是

*
*
*
*
*
*

当我输入 3 时,这是我的代码:

package square_display;
import java.util.Scanner;

public class Square_display

{
public static void main(String[] args)
{

Scanner input= new Scanner(System.in);

System.out.print("Please enter positive number: ");
int number =input.nextInt();


while (number > 15 || number<=0)
{
System.out.println("Don't input value greater than 15"
+ " and negative number");
System.out.print("Enter number again: ");
number=input.nextInt();

}
for(int row=0; row<number;row++)
{
for (int colum = 0; colum < number ; colum++)
{
System.out.println("X");
}

}

感谢您的帮助

最佳答案

因为您使用的是println,所以它会将每个字符放在单独的行上。您需要在内循环中使用 print 并在外循环中使用空白 println

for(int row=0; row<number;row++)
{
for (int colum = 0; colum < number ; colum++)
{
System.out.print("X");
}
System.out.println();
}

关于java - Java中的方形显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36003274/

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