Can you reduce the double for statement?

Asked 2 years ago, Updated 2 years ago, 83 views

 for(inti = 1; i <= 1; i++) {//y-axis

            for(int j = (char')A'; j <= (char')D'; j++) {//x-axis

                System.out.print((char)j + " ");

            }//inner

            System.out.println();//Rewind Line

        }//outer

        for(inti = 1; i <= 1; i++) {//y-axis

            for(int j = (char')E'; j <= (char')H'; j++) {//x-axis

                System.out.print((char)j + " ");

            }//inner

            System.out.println();//Rewind Line

        }//outer

        for(inti = 1; i <= 1; i++) {//y-axis

            for(int j = (char')I'; j <= (char')L'; j++) {//x-axis

                System.out.print((char)j + " ");

            }//inner

            System.out.println();//Rewind Line

        }//outer

    }//main

}

Is there a way to reduce this more briefly?

Please help me because I can't get the answer.

java for

2022-09-20 20:48

1 Answers

I don't know Java well, so there may be grammatical errors, but please look at the basic ideas.

for ( int j = (char)'A'; j <= (char)'L'; j++) {
    System.out.print((char)j + "); // Do what you need to do for all indexes.
    if (j == 'D' || j == 'H' || j == 'L') { // in a specific index
        System.out.println(); // Do what you want to do.
    }
}


2022-09-20 20:48

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.