简体   繁体   English

使用for循环在Java中创建二维坐标平面?

[英]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.我正在尝试创建一个程序 CoordinateFinder.java,它向用户询问 1 到 5 之间的两个整数值。然后,使用一对 for 循环来生成一个 2D 坐标平面。 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.平面应该为平面上的每个坐标打印一个句点,用户指定的坐标除外,应该打印一个 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.在嵌套的FOR循环中,变量i代表您的y值,变量j代表您的x值。 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).因此,在每个i循环中,您需要打印完整的行(y 值),在每个 'j' 嵌套循环中,您需要确定每列中打印的内容(x 值)。 To determine if you need to print an X or a .要确定是否需要打印X. you need to compare i and j to y and x , like so:您需要将ijyx进行比较,如下所示:

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.请注意,当您在网格上从左到右遍历时,我确实反转了子循环中j的方向以表示增加的 x 值。 I left i decrementing to represent the decreasing y-values as you traverse down the rows.我离开i递减以表示当您向下遍历行时递减的 y 值。 The rest of my changes were formatting.我的其余更改是格式化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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