gpt4 book ai didi

java - 带有常量变量的 For() 循环不打印任何输出

转载 作者:行者123 更新时间:2023-12-02 01:39:43 25 4
gpt4 key购买 nike

问题是没有发生输出,而不是额外的 println()。这很奇怪,因为在没有静态 SIZE var 的情况下进行此编程,它工作得很好。

public class SlashFigure2
{
public static final int SIZE = 4;
public static void main(String[] args)
{
for(int i = 1; i <= SIZE; i++)
{
for(int j = 1; j <= 2 * i - (2 * SIZE + 2); j++)
{
System.out.print("\\");
}

for(int j = 1; j <= -4 * i + (-4 * SIZE + 2); j++)
{
System.out.print("!");
}

for(int j = 1; j <= 2 * i - (2 * SIZE + 2); j++)
{
System.out.print("/");
}

System.out.println();
}
}

}

如果有人需要它,以下是程序打印的内容:

!!!!!!!!!!!!!!
\\!!!!!!!!!!//
\\\\!!!!!!////
\\\\\\!!//////

编辑:Here's what the site keeps saying is the error

编辑2:该网站是practiceit.csu.washington.edu

问题的措辞如下:

Modify your DollarFigure program from the previous exercise to becomea new program called DollarFigure2 that uses a global constant for thefigure's height. (You may want to make loop tables first.) Theprevious output used a constant height of 7. The outputs below use aconstant size of 3 (left) and 5 (right)

Here are the outputs below they are talking about

(You must solve this problem using only ONE public static finalconstant, not multiple constants; and its value must be used in theway described in this problem.)

最佳答案

只需这样做:

if (i != SIZE) {
System.out.println();
}

因为在最后一次迭代中i将等于SIZE,并且在这种情况下您想跳过println()

更新

从注释和图像中,很明显您不应该将 SIZE 定义为常量,显然您应该能够将 n 作为参数传递对于您的程序来说,它不是硬编码值。检查您一直引用的“站点”的规则,应该如何接收输入?

关于java - 带有常量变量的 For() 循环不打印任何输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54611280/

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