简体   繁体   English

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

[英]For() loop with constant variable isn't printing any output

The problem is that there is no output happening, not an extra println().问题是没有输出发生,没有额外的 println()。 This is odd, because doing this programming without a static SIZE var, it works just fine.这很奇怪,因为在没有静态SIZE情况下进行此编程,它工作得很好。

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();
        }      
    }  
    
}

In case anyone needs it, here's what the program prints:如果有人需要它,这是程序打印的内容:

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

EDIT: Here's what the site keeps saying is the error编辑:这是网站一直说的错误

EDIT 2: The site is practiceit.csu.washington.edu编辑 2:该网站是 practiceit.csu.washington.edu

Here is the question's wording:这是问题的措辞:

Modify your DollarFigure program from the previous exercise to become a new program called DollarFigure2 that uses a global constant for the figure's height.将之前练习中的 DollarFigure 程序修改为名为 DollarFigure2 的新程序,该程序使用图形高度的全局常量。 (You may want to make loop tables first.) The previous output used a constant height of 7. The outputs below use a constant size of 3 (left) and 5 (right) (您可能想先制作循环表。)前面的输出使用了 7 的恒定高度。下面的输出使用了 3(左)和 5(右)的恒定大小

Here are the outputs below they are talking about以下是他们正在谈论的输出

(You must solve this problem using only ONE public static final constant, not multiple constants; and its value must be used in the way described in this problem.) (你必须只使用一个公共静态最终常量来解决这个问题,而不是多个常量;并且它的值必须按照这个问题中描述的方式使用。)

Simply do this:只需这样做:

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

Because i will be equal to SIZE in the last iteration, and you want to skip the println() in that case.因为在最后一次迭代中i将等于SIZE ,在这种情况下您想跳过println()

UPDATE更新

From the comments and the image, it's clear that you're not supposed to define SIZE as a constant, apparently you should be able to pass n as a parameter to your program, it's not a hardcoded value.从注释和图像中,很明显您不应该将SIZE定义为常量,显然您应该能够将n作为参数传递给您的程序,它不是硬编码值。 Check the rules of the "site" you keep referring to, how's the input supposed to be received?检查您一直提到的“站点”的规则,应该如何接收输入?

You can make this change in your code to make it work.You should not execute the statement when i is equal to SIZE您可以在代码中进行此更改以使其工作。当i等于SIZE时,您不应执行该语句

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

Somewhat Jeopardy to find out the actual problem/quest you want to solve by algorithmically print a specific ASCII-art only denoted by a constant ROW-size (eg 4 or 6 as depicted on the attached image).通过算法打印仅由恒定ROW 大小表示的特定 ASCII 艺术(例如,如附图所示为 4 或 6),找出您想要解决的实际问题/任务有点危险

Tests & sample output测试和样本输出

在此处输入图片说明

Derived specification衍生规格

Draw a specific figure varying only in its height :绘制一个特定的图形,仅在其高度上有所不同:

  • only single parameter is passed: rows of ASCII-art to draw只传递一个参数:要绘制的 ASCII 艺术
  • figure to draw should resemble a downward-arrow要绘制的图形应类似于向下箭头
  • bordered by double-slashes left and right, ie \\\\ respective //以左右双斜线为边界,即\\\\各自的//
  • no border/slashes on the first row第一行没有边框/斜线
  • inner/rest of the rows filled with exclamation-marks !!内部/其余行充满感叹号!!
  • at least 2 exclamation-marks !!至少2个感叹号!! on the inner last row在最后一行内

Java method with single parameter: ROWS带单参数的 Java 方法:ROWS

private static void drawAsciiArt(int rows) {
    int columns = (rows-1)*4+2;

    for(int i = 1; i <= rows; i++) {
        int borderSize = (i-1)*2;
        int fillSize = columns - borderSize*2;

        for(int j = 1; j <= borderSize; j++) {
            System.out.print("\\");
        }

        for(int j = 1; j <= fillSize; j++) {
            System.out.print("!");
        }

        for(int j = 1; j <= borderSize; j++) {
            System.out.print("/");
        }

        if (i < rows) {
            System.out.println();
        } // if not last row

    } // end of row-loop
}

Try this online网上试试这个

Figured it out!弄清楚了! It turns out that for both the '\\' and '/' characters, I didn't need to use that (x * SIZE + y) formula after all.事实证明,对于 '\\' 和 '/' 字符,我根本不需要使用那个 (x * SIZE + y) 公式。 They both needed the regular formula while the '!'他们都需要常规公式,而“!” is the only character that needed the SIZE formula是唯一需要 SIZE 公式的字符

public class SlashFigure2
{
    public static final int SIZE = 4;
    //program works no matter what value SIZE holds
    public static void main(String[] args)
    {
        for(int i = 1; i <= SIZE; i++)
        {
            for(int j = 1; j <= 2 * i - 2; j++)
            {
                System.out.print("\\");
            }
            
            //note the SIZE formula in here
            for(int j = 1; j <= -4 * i + (4 * SIZE + 2); j++)
            {
                System.out.print("!");
            }
            
            for(int j = 1; j <= 2 * i - 2; j++)
            {
                System.out.print("/");
            }
            
            System.out.println();
        }      
    }  
    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM