簡體   English   中英

Java 程序打印一個 x & y 坐標圖並顯示一個標繪點

[英]Java program to print an x & y coordinate graph and displayed a plotted point

我必須打印一個 6 x 6 圖表和 plot 給定點。 這是 x=2 和 y=3 的示例:

 5 . . . . . 
 4 . . . . . 
 3 . x . . . 
 2 . . . . . 
 1 . . . . . 
 0 1 2 3 4 5 

我的代碼:

public static String[][] plotPoint(String[][] plot){
    int x=0, y=0;
    Scanner sc = new Scanner(System.in);
    boolean bool = true;
    while(bool) {
        System.out.println("Enter x coordinate between 0 and 5: ");
        x = sc.nextInt();
        System.out.println("Enter y coordinate between 0 and 5: ");
        y = sc.nextInt();

        if(x>=5||x<0||y>=5||y<0) {
            System.out.println("The coordinates entered exceed the limit.");
        }else {
            x = x-1;
            bool=false;
        }
    }
    for(int i=0; i<6; i++) {
        for(int j = 0; j<6; j++) {
            if(i==x && j==y) {

                plot[x][y]="x";//assigning "x" to coordinates
                break;
            }
        }
    }
    return plot;

}

 public static void main(String [] args) {
    int h = 5;
    Scanner sc = new Scanner(System.in);
    String [][] strArr = new String[6][6];
    char c='.';
    for(int i = 0; i<6; i++) {
        for(int j = 0; j<6; j++) {
            if(j==0||i==5) {
                strArr[i][j] = Integer.toString(h);
                if(i==5)h++;
                else h--;
            }
            else {
                strArr[i][j]= Character.toString(c);
            }
        }
    }

    String[][] nStrArr = new String[6][6];
    nStrArr = plotPoint(strArr);
    for(int i = 0; i<6; i++) {
        for(int j = 0; j<6; j++) {
            System.out.print(nStrArr[i][j]+ " ");
        }
        System.out.println();
    }
}

目前它正在正確打印圖表,但該點完全錯誤,我認為我的 plotPoint 方法中的第二個 if 語句不正確,但我不確定還能嘗試什么。

我認為不需要for循環在圖中標記x 嘗試這個。

public static String[][] plotPoint(String[][] plot) {
    int x = 0, y = 0;
    Scanner sc = new Scanner(System.in);
    boolean bool = true;
    while (bool) {
        System.out.println("Enter x coordinate between 0 and 5: ");
        x = sc.nextInt();
        System.out.println("Enter y coordinate between 0 and 5: ");
        y = sc.nextInt();

        if (x >= 5 || x < 0 || y >= 5 || y < 0) {
            System.out.println("The coordinates entered exceed the limit.");
        } else {
            y = y + 1;
            bool = false;
        }
    }
    plot[plot.length - y][x] = "x";
    return plot;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM