gpt4 book ai didi

java - Eclipse 不会打印我的代码中的空格

转载 作者:行者123 更新时间:2023-12-02 04:47:50 26 4
gpt4 key购买 nike

我的问题出在最中心的 while 循环上。该程序的目标是打印一个由星号制成的盒子,其尺寸由用户提供,并且效果相当好。但是,中心 while 循环不会打印空白以使其自身保持距离。

这可能是什么原因造成的?

import java.util.Scanner;


public class bonus {

/**
* @param args
*/
public static void main(String[] args) {
Scanner widthScan = new Scanner(System.in);
System.out.println("Enter the width: ");
int width = widthScan.nextInt();

Scanner heightScan = new Scanner(System.in);
System.out.println("Enter the height: ");
int height = heightScan.nextInt();

int resetWidth = width;
///////////////////////////////////////////////////////////opening width sandwich
while(width>=1)
{
System.out.print("*");
width--;
}
System.out.println();
///////////////////////////////////////////////////////////

int specialHeight = height-2;
int specialWidth = width-2;
while(specialHeight>=1)
{
System.out.print("*");

while(specialWidth>=1)
{
System.out.println(" ");
specialWidth--;
}

System.out.print("*");
specialHeight--;
System.out.println();
}

////////////////////////////////////////////////////////////

while(resetWidth>=1)
{
System.out.print("*");
resetWidth--;
}
////////////////////////////////////////////////////////////closing width sandwich
}
}

最佳答案

此循环之后:

while(width>=1)

width 显然等于或小于 0。

接下来你要做的:

int specialWidth = width-2;

因此 specialWidth 为 -2 或更小。

然后你就有了:

while(specialWidth>=1)

期望如何进入该循环?您期望 specialWidth 是什么?目前尚不清楚您打算如何初始化 specialWidth (也许是 width 的初始值?),但这可能是需要查看的地方。

现在也是学习使用调试器的好时机,这样您就可以单步执行代码来弄清楚发生了什么(如果您无法通过检查来做到这一点)。

关于java - Eclipse 不会打印我的代码中的空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29520829/

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