-1

The problem is that there is no output happening, not an extra println(). This is odd, because doing this programming without a static SIZE var, it works just fine.

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

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. (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)

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.)

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
CurlyFry
  • 37
  • 6
  • You should [edit] the question so that it includes the [image](https://imgur.com/a/ZQNkpPN) you wrote below one of the answers. The screenshot says: the expected output consists of 4 lines, your program only printed 1 (or maybe even 0, that's hard to say). Read the instructions again. Maybe you were supposed to write the output to a file instead of `System.out`. – Roland Illig Feb 09 '19 at 22:21
  • 1
    does it really print anything despite empty lines? `2 * i - (2 * SIZE + 2)` with `i<=SIZE` is negative....same for `-4 * i + (-4 * SIZE + 2)` the loops with `j` will never loop – user85421 Feb 09 '19 at 22:27
  • correct. this program doesn't print anything. So the output shown in the question is not this program's output. Thanks. – kai Feb 09 '19 at 22:31
  • only empty lines, confirmed: https://www.ideone.com/DHAQcS and is also what the `diff` on the site is showing (empty lines probably being discarded) – user85421 Feb 09 '19 at 22:33
  • @RolandIllig You're right! I just ran it through a different compiler, there's no output happening! This is weird, because without using a constant variable, all the code runs fine – CurlyFry Feb 09 '19 at 22:40
  • @CurlyFry I think you should not use a constant SIZE, my guess is that you're expected to pass `n` to your program – Óscar López Feb 09 '19 at 22:44
  • 1
    probably another wrong interpretation. would be nice to have the correct code, and not something just similar. So now it is a different compiler , no, it is the use of constant, ... or maybe it is just the wrong code (the formula doesn't care if it is a constant or a parameter, will always be negative given SIZE > 0) – user85421 Feb 09 '19 at 22:44
  • If it helps, after running a debug, it's skipping all the inner J loops completely. I would think it would be because the conditions, no? Or perhaps the SIZE formulas used in the J loops – CurlyFry Feb 09 '19 at 22:48
  • SEE MY FIRST COMMENT ABOVE (the 2nd comment after the question) - *the loops with j will never loop* – user85421 Feb 09 '19 at 22:49
  • @CurlyFry As you just experienced, as long as you don't tell us the full story (including the "site", the exact wording of the task, the exact code you are submitting, and so on) you can only get best guesses from us, but no real help. – Roland Illig Feb 09 '19 at 22:49
  • @RolandIllig Edited post to enter that information – CurlyFry Feb 09 '19 at 22:55
  • @CarlosHeuberger After writing the math down, those for loops should be executing. When i = 1 on the second loop, j <= -18 (after plugging in i and SIZE) – CurlyFry Feb 09 '19 at 22:56
  • sure not, how can `j` be `<= -18` (negative 18 , what ever math gives that result) if it starts being `1` (positive one) ??? And your output is not what the site is asking for - wrong problem? or still wrong code? – user85421 Feb 09 '19 at 22:59
  • @CarlosHeuberger Let's assume i = 1 and SIZE = 4. j <= -4 * i + (-4 * SIZE + 2) j <= -4 * 1 + (-4 * 4 + 2) j <= -4 + (-16 + 2) j <= -4 + -14 j <= -18 – CurlyFry Feb 09 '19 at 23:02
  • the above is the 3rd loop... , the second is`2 * i - (2 * SIZE + 2)` == `2 * 1 - (2 * 4 + 2)` == `2 - (8 + 2)` == `2 - 10` == `-8`, but doesn't matter, `j` will never be less than any negative number when starting positive and being incremented (unless there is an overflow) – user85421 Feb 09 '19 at 23:02
  • @CarlosHeuberger Ohhh I'm so sorry. I see your point now. Lemme fix it – CurlyFry Feb 09 '19 at 23:03
  • @CarlosHeuberger So I now have output, all I had to do was reverse the positive/negative on all 3 loops. [Here is the output now](https://imgur.com/a/sWnNLkS) How can I change this to even it out? – CurlyFry Feb 09 '19 at 23:31
  • `j <= 2 * i - (2 * SIZE + 2)` is always negative, as well as loop conditions in other loops. –  Feb 09 '19 at 23:48

4 Answers4

2

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.

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. Check the rules of the "site" you keep referring to, how's the input supposed to be received?

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Site is still marking me wrong. I think you're 100% right, but I'm not sure why it still says my code is creating an extra println... [here's what the site keeps saying](https://imgur.com/a/ZQNkpPN) – CurlyFry Feb 09 '19 at 22:17
0

You can make this change in your code to make it work.You should not execute the statement when i is equal to SIZE

if(i<SIZE){
    System.out.println();
    }
Ratish Bansal
  • 1,982
  • 1
  • 10
  • 19
0

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 (e.g. 4 or 6 as depicted on the attached image).

Tests & sample output

enter image description here

Derived specification

Draw a specific figure varying only in its height:

  • only single parameter is passed: rows of ASCII-art to draw
  • figure to draw should resemble a downward-arrow
  • bordered by double-slashes left and right, i.e. \\ respective //
  • no border/slashes on the first row
  • inner/rest of the rows filled with exclamation-marks !!
  • at least 2 exclamation-marks !! on the inner last row

Java method with single parameter: 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

hc_dev
  • 8,389
  • 1
  • 26
  • 38
0

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. They both needed the regular formula while the '!' is the only character that needed the SIZE formula

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();
        }      
    }  
    
}
CurlyFry
  • 37
  • 6