waifu_nom
waifu_nom

Reputation: 33

How to Reverse an Int with For Loops

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

Upvotes: 0

Views: 481

Answers (4)

Tanmay Naik
Tanmay Naik

Reputation: 658

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

Upvotes: 0

Amongalen
Amongalen

Reputation: 3131

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

Upvotes: 1

Nowhere Man
Nowhere Man

Reputation: 19545

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-- + " ");
}

Upvotes: 1

TreffnonX
TreffnonX

Reputation: 2930

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.

Upvotes: 0

Related Questions