具有常量变量的For()循环不会输出任何输出

时间:2019-02-09 22:10:42

标签: java ascii-art

实际的问题是没有输出发生,没有多余的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

这是问题的措辞:

“将上一个练习中的DollarFigure程序修改为一个名为DollarFigure2的新程序,该程序使用全局常量作为图形的高度。(您可能要先创建循环表。)先前的输出使用7的恒定高度。下面的输出使用3(左)和5(右)的恒定大小

Here are the outputs below they are talking about

(您必须仅使用一个公共静态最终常量来解决此问题,而不能使用多个常量;并且其值必须以该问题中所述的方式使用。)”

4 个答案:

答案 0 :(得分:2)

只需执行以下操作:

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

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

更新

从注释和图像中可以很明显地看出,您不应该将SIZE定义为常量,显然您应该能够将n作为参数传递给您的程序,而不是硬编码的值。检查您不断引用的“站点”的规则,应该如何接收输入?

答案 1 :(得分:0)

您可以在代码中进行更改以使其正常工作。i等于SIZE时不应执行该语句

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

答案 2 :(得分:0)

某种程度上 Jeopardy 可以通过算法打印只用恒定 ROW-size 表示的特定ASCII艺术来找出您想要解决的实际问题/要求(例如4或6,如图所示)。

测试和样品输出

enter image description here

派生规范

绘制一个特定图形,该图形仅在其高度中变化:

    仅传递
  • 单个参数:要绘制的 ASCII艺术图形
  • 绘制图形应类似于向下箭头
  • 以左右两个双斜线为界,即\\分别为//
  • 第一行没有边框/斜杠
  • 用感叹号!!填充的其余行的内部/其余部分
  • 在最后一行的内部至少
  • 至少有2个感叹号!!

具有单个参数的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

答案 3 :(得分:0)

想通了!事实证明,对于'\'和'/'字符,我根本不需要使用(x * SIZE + y)公式。他们都需要常规公式,而'!'是唯一需要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();
        }      
    }  
    
}