0

i am having trouble with reversing this code. what i am trying to have as

this is what i have so far but i can't seem to wrap my head around how the third for loop is supposed to be

public static void main(String[] args) {

    Scanner input = new Scanner(System.in); //gets the users input

    int rows;
    int number = 0;
    int i = 0;
    rows = input.nextInt(); //takes the users input from console 


    while (rows <= 0) {
        System.out.println("INVALID");
        rows = input.nextInt();
    }


    for (int c = 1; c <= rows; c++) {

            for (i = 0; i < c; i++) {
                System.out.print(++number + " ");
            }        

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

            for(i = 0; i < c; i++) {
                System.out.print(number + " ");
                //number--;
            }

            System.out.println();   
    }
3
  • What is the logic here ? How input of 5 is converted to this output ? Commented May 4, 2020 at 9:35
  • What about System.out.print(number-- + " ");? And you probably need to keep track of the highest value in line and use it as a starting value in the next one. Commented May 4, 2020 at 9:35
  • but the numbers get messed up if i do that Commented May 4, 2020 at 9:38

4 Answers 4

1

Before running your last loop you should store number in some temp variable:

int temp = number;

for(i = 0; i < c; i++) {
    System.out.print(temp-- + " ");
}
Sign up to request clarification or add additional context in comments.

Comments

1

As I said in the comment, you need to decrement the number but at the same time need to keep track of the highest values in a line to use it as a starting value in the next iteration. Something like this should work:

public static void main(String[] args) {
  int rows;
  int number = 0;
  int highestValue = 0;
  int i = 0;
  rows = 5; 

  for (int c = 1; c <= rows; c++) {
      number = highestValue; // reset number to the highest value from previous line
      for (i = 0; i < c; i++) {
          System.out.print(++number + " ");
      }
      highestValue = number; // setting the highest value in line

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

      for(i = 0; i < c; i++) {
          System.out.print(number-- + " "); // decrementing 
      }

      System.out.println();   
  }

Comments

0

Do you have to implement this yourself, because otherwise there are tons of libraries handling arrays.

The steps you have to take are:

  • Find a way to read the input (integers) in a single line and store them in some kind of container (either an array, or a list).
    • You may have to isolate what a single integer is (e.g. "1 2 3" is 3 integers, which you want to reverse, while "12 3" is just 2, and you only want to reverse 2).
    • You need to ensure that your input is valid (e.g. a user may enter "1 a b c")
  • You need to flip the integers within the container or better copy the original container in reverse order. For this, you only need to iterate over the container's elements and add them to the target container in reverse order
for (Integer in : inputList) {
    outputList.addFirst(in);
}

If you only want to print the integers, you don't have to store them in a list, you could just iterate over the container in reverse order.

2 Comments

I think you've overcomplicating it a little bit. This seems to be a simple "print pattern" exercise.
I like the approach of giving a blueprint but your solution in general seems to be too complicated. As shown in my and the other asnwers, the result can be achieved by adding a single int variable. No need to store all the numbers, reverse containers, join them together and so on. It will most likely work, just it may be too much for someone learning programming.
0

It seems to bit a pattern program, you can add the number-- in you sysout

 public static void main( String[] args ) 
    {
        Scanner input = new Scanner(System.in); //gets the users input

        int rows;
        int number = 0;
        int i = 0;

        rows = input.nextInt(); //takes the users input from console 


        while (rows <= 0) {
            System.out.println("INVALID");
            rows = input.nextInt();
        }


        for (int c = 1; c <= rows; c++) {

                for (i = 0; i < c; i++) {
                    System.out.print(++number + " ");
                }        

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

                for(i = 0; i < c; i++) {
                    System.out.print(number-- + " ");
                    //number--;
                }

                System.out.println();   
        }
    }   

enter image description here Such type of pattern you can create through collections

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.