简体   繁体   中英

Creating a 2D Co-Ordinate plane in Java using for loops?

I am trying to create a program CoordinateFinder.java that asks the user two integer values between 1 and 5. Then, use a pair of for loops to produce a 2D coordinate plane. The plane should print a period for every coordinate on the plane except for the one specified by the user, which should print an X.

Example of what I am trying to do:

Enter your x coordinate: 
2
Enter your y coordinate: 
4

5 . . . . . 
4 . X . . . 
3 . . . . . 
2 . . . . . 
1 . . . . . 
0 1 2 3 4 5 

What I have:

import java.util.Scanner;
public class CoordinateFinder {

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

System.out.println("Please enter an X co-ordinate from 1-5: ");
int x = input.nextInt();

System.out.println("Please enter a y co-ordinate from 1-5:  ");
int y = input.nextInt();


for (int i = 5; i >= 1; i--) {
    System.out.print(i +" ");
    if (i == 0) {
        System.out.println("\n");
        System.out.println(" 5 4 3 2 1 ");  
    }
    for (int j = 4; j >= 0; j--) {
        System.out.print(" . ");
        if (j == 0) {
            System.out.println("\n");

        }
    }
}
}

}

Which outputs:

5  .  .  .  .  . 

4  .  .  .  .  . 

3  .  .  .  .  . 

2  .  .  .  .  . 

1  .  .  .  .  . 

0 

5 4 3 2 1 
.  .  .  .  . 

In your nested FOR loops, the variable i represents your y value and variable j represents your x value. So on each i loop you need to print the complete row (y-values) and on each 'j' nested loop you determine what is printed in each column (x-value). To determine if you need to print an X or a . you need to compare i and j to y and x , like so:

import java.util.Scanner;

public class CoordinateFinder {

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

    System.out.println("Please enter an X co-ordinate from 1-5: ");
    int x = input.nextInt();

    System.out.println("Please enter a y co-ordinate from 1-5:  ");
    int y = input.nextInt();

    System.out.println("");

    for (int i = 5; i >= 1; i--) {
        System.out.print(i);

        for (int j = 1; j <= 5; j++) {
            if (i == y && j == x) {
                System.out.print(" X");
            } else {
                System.out.print(" .");
            }
        }

        System.out.print("\n");
    }

    System.out.println("0 1 2 3 4 5");
}

}

Output:

Please enter an X co-ordinate from 1-5: 
2
Please enter a y co-ordinate from 1-5:  
4

5 . . . . .
4 . X . . .
3 . . . . .
2 . . . . .
1 . . . . .
0 1 2 3 4 5

Please note that I did reverse the direction of j in the subloop to represent the increasing x-values as you traverse from left to right on the grid. I left i decrementing to represent the decreasing y-values as you traverse down the rows. The rest of my changes were formatting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM