-1

I need to write a program in Eclipse that displays "*" in a line of seven and then takes one off each line while also using nested loops.

I've tried using examples that the teacher provided and then adapting it to use an "*".

This is the code I've been trying to use

public class ForWhileLoopsPractice {

    public static void main(String[] args) {

        int rows = 7;
        int asterisk = '*';

        for(int i = asterisk; i <= rows; i++ ) {
            for(int j = asterisk; j >= i; j--) {
                System.out.print(j + " ");
            }
            System.out.println(" ");
        }
    }
}

This just terminates automatically and I don't think it will even go in the right direction if it works.

The end result should look like

"*******"
"******"
"*****"
"****"
"***"
"**"
"*"

without the quotes around each one and just the asterisk but I have been unable to produce anything close to this.

Thank you for all the help so far. Now my code looks like this

int rows = 7;

    for(int i = 0; i < rows; i++ ) 
    {
        for(int j = 0; j < rows; j++) 
        {
           System.out.print("*");
        }
        System.out.println("*");
    }

Output is now

********
********
********
********
********
********
********

I just need to find a way to subtract one from each row.

Thank you to @an3rror, the solution ended up being

int rows = 7;
    int columns = 7;
    for(int i = 0; i < rows; i++ )
    {
        for(int j = 0; j < columns; j++)
        {
           System.out.print("*");

        }
        System.out.println();
        columns--;
    }

Thank you everyone who replied for giving me tips, without just outright saying the answer, and explaining what each thing was and why it was that way.

AJ Mosley
  • 11
  • 5
  • 6
    This is very interesting `int asterisk = '*';` - what do you think that does? – Scary Wombat Nov 07 '19 at 00:26
  • 3
    Instead of printing `j + " "`, print `"*"`. And your first loop should be from `0` to `rows`, and the second should be from `0` to `rows - i`. – Elliott Frisch Nov 07 '19 at 00:26
  • Why are you trying to use an asterisk as a numeric value? Keep your numbers as numbers and your strings as strings and this will likely be a lot easier. – David Nov 07 '19 at 00:26
  • ...and your chars as chars! – csabinho Nov 07 '19 at 00:32
  • So what output you are getting from this? – Kishan Bheemajiyani Nov 07 '19 at 00:36
  • I agree now that I look at it more closely. My teacher said it looked good so far so I went with it, but now it doesn't make sense. @Elliot Frisch What do you mean by have it go from `0` to `rows`? I don't understand what you mean by that. – AJ Mosley Nov 07 '19 at 00:36
  • 1
    `for(int i = 0; i < rows; i++ )` is going from 0 to `rows` – Scary Wombat Nov 07 '19 at 00:43
  • @ScaryWombat Thank you for clarifying. That made it so the program prints out 7 asterisks in 1 column, what else am I missing to make it like the end result. I assume it has to do with the second for loop. – AJ Mosley Nov 07 '19 at 00:48
  • Did you read the other comments, and implement the changes suggested? If so edit your question with the latest code. – Scary Wombat Nov 07 '19 at 00:51

2 Answers2

0
    int rows = 7;
    int numOfChars = 7;


    for (int i = 0; i < rows; i++) {
        for (int j = 0; j < numOfChars; j++) {
            System.out.print("*");
        }
        System.out.println();
        numOfChars--;
    }
Kevin
  • 32
  • 2
  • Thank you, but could you explain a bit why I need the numOfChars line? I assume it is to make sure a column of asterisks is subtracted for each line? – AJ Mosley Nov 07 '19 at 01:05
  • Right now you're using the number of rows (7) as the amount of asterisks to put in each row, and you're never changing the value from 7. That's why every line has 7 asterisks. You needed a number of asterisks that you reduced by one for each line. – Kevin Nov 07 '19 at 01:07
  • That makes so much sense, thank you so much for your answer, it really helped me wrap my head around what my teacher wanted us to do. – AJ Mosley Nov 07 '19 at 01:11
  • 1
    Suggest changing to `int numOfChars = rows;` – Scary Wombat Nov 07 '19 at 01:12
-1

You can set the value of "rows" when you're assigning to for loop index;

example and equation below:

for(int i = 0; i <= 6; i++) {
    for(int j = 1; j <= 7 -i; j++)
        System.out.print("*");
    System.out.println();
}

Simpler version and I hope this helped.

edit; no "magic numbers"

    int rows = 6;

for(int i = 0; i <= rows; i++) {
    for(int j = 1; j <= rows -i; j++)
        System.out.print("*");
    System.out.println();
}
  • 1
    Thank you for your answer, but my teacher likes us to kind of say exactly what the program will do if that makes sense? I'm in a computer science class in my High School, so she wants us to really understand what we are doing and why. – AJ Mosley Nov 07 '19 at 01:10
  • Using variables to avoid [magic numbers](https://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) usually makes the code easier to understand – Emmanuel Chebbi Nov 07 '19 at 09:04
  • Then s/ he can do; int rows = 6; for(int i = 0; i <= rows; i++) { for(int j = 1; j <= rows -i; j++) System.out.print("*"); System.out.println(); } It's the same exact thing and much simpler – sey.cileli Nov 16 '19 at 23:17