【问题标题】:Java : Drawing using a two dimensional array as a gridJava:使用二维数组作为网格进行绘图
【发布时间】:2014-01-20 10:09:50
【问题描述】:

我正在使用二维数组来表示使用 (X,Y) 坐标的网格。网格为 80 列 x 30 行。我正在使用重定向 (java project1

project1.input 如下所示:

c
l 10 10 20 10 *
l 20 10 20 20 *
l 20 20 10 20 *
l 10 20 10 10 *
p 15 15 X
d
q

c = clear(重新初始化)

l = 使用字符 (*) 从 X1 Y1 到 X2 Y2 画一条线

p = 使用 char ( X ) 在 X1 Y1 处绘制一个点

d = 打印当前网格

q = 退出

这是我的整个 .java,由于某种原因,它只能绘制一个点而不能打印线。我已经尝试查看我的 for 循环来设置点,我的 getInput 方法,但我找不到问题。

import java.util.Scanner;
import java.lang.*;

public class project1
{
    // declaring class array named grid
    public static char[][] grid = new char[80][30];

    // method: main
    // purpose: calls getInput and initGrid

    public static void main(String[] args)
    {
    initGrid();
    getInput();
    }

    // method: initGrid
    // purpose: initializes the grid with spaces, also acts as the clear command
    public static void initGrid()
    {
    char fill = '1';
    for(int i = 0; i < grid.length; i++){
        for(int j = 0; j < grid[0].length; j++){
        grid[i][j] = fill;
        }
    }
    }

    // method: getInput
    // purpose: gets the input and sends it to the appropriate method
    public static void getInput()
    {
    char firstCharacter, drawCharacter;
    int X1, Y1, X2, Y2;
    Scanner in = new Scanner(System.in);
    while(in.hasNextLine()){
        firstCharacter = in.next().charAt(0);
        if(firstCharacter == 'p'){
        X1 = in.nextInt();
        Y1 = in.nextInt();
        drawCharacter = in.next().charAt(0);
        in.nextLine();
        plotPoint(X1, Y1, drawCharacter);
        }
        if(firstCharacter == 'l'){
        X1 = in.nextInt();
        Y1 = in.nextInt();
        X2 = in.nextInt();
        Y2 = in.nextInt();
        drawCharacter = in.next().charAt(0);
        in.nextLine();
        drawLine(X1, Y1, X2, Y2, drawCharacter);
        }
        if(firstCharacter == 'c'){
        in.nextLine();
        initGrid();
        }
        if(firstCharacter == 'd'){
        in.nextLine();
        printGrid();
        }
        if(firstCharacter == 'q'){
        return;
        }
    }

    }

    // method: plotPoint
    // purpose: receives the p command and plots the point with given coordinates and char
    public static void plotPoint(int X, int Y, char character)
    {
    grid[X][Y] = character;
    }

    // method: drawLine
    // purpose: receives the l command and draws a line with given coordinates and char
    public static void drawLine(int X1, int Y1, int X2, int Y2, char character)
    {
    int stopX = X1;
    int stopY = Y1;
    int stopX2 = X2;
    int stopY2 = Y2;
    // checks for horizontal line, left to right
    if(Y1==Y2 && X1<X2){
        for(int i = 0; stopX == stopX2; i++){ 
        grid[X1+i][Y1] = character;
        }
    }

    // checking for horizontal line, right to left
    if(Y1==Y2 && X1>X2){
        for(int i = 0; stopX2 == stopX; i--){
        grid[X1+i][Y1] = character;
        }
    }

    // checking for vertical line, top to bottom
    if(X1==X2 && Y1<Y2){
        for(int i = 0; stopY == stopY2; i++){
        grid[X1][Y1+i] = character;
        }
    }

    // checking for vertical line, bottom to top
    if(X1==X2 && Y1>Y2){
        for(int i = 0; stopY2 == stopY; i--){
        grid[X1][Y1+i] = character;
        }
    }   
    }

    // method: printGrid
    // purpose: prints the grid to show the picture
    public static void printGrid()
    {
    for(int j = 0; j < grid[0].length; j++){
        for(int i = 0; i <  grid.length; i++){
        System.out.print(grid[i][j]);
        if(i == grid.length-1){
            System.out.println();
        }

        }
    }
    }

}

对不起括号,它不能很好地从emacs中复制出来。

【问题讨论】:

    标签: java grid multidimensional-array


    【解决方案1】:

    您的 for 循环不起作用,因为条件错误!

    import java.util.Scanner;
    import java.lang.*;
    
    public class Grid
    {
    // declaring class array named grid
    public static char[][] grid = new char[80][30];
    
    // method: main
    // purpose: calls getInput and initGrid
    
    public static void main(String[] args)
    {
    initGrid();
    getInput();
    }
    
    // method: initGrid
    // purpose: initializes the grid with spaces, also acts as the clear command
    public static void initGrid()
    {
    char fill = '1';
    for(int i = 0; i < grid.length; i++){
        for(int j = 0; j < grid[0].length; j++){
        grid[i][j] = fill;
        }
    }
    }
    
    // method: getInput
    // purpose: gets the input and sends it to the appropriate method
    public static void getInput()
    {
    char firstCharacter, drawCharacter;
    int X1, Y1, X2, Y2;
    Scanner in = new Scanner(System.in);
    while(in.hasNextLine()){
        firstCharacter = in.next().charAt(0);
        if(firstCharacter == 'p'){
        X1 = in.nextInt();
        Y1 = in.nextInt();
        drawCharacter = in.next().charAt(0);
        in.nextLine();
        plotPoint(X1, Y1, drawCharacter);
        }
        if(firstCharacter == 'l'){
        X1 = in.nextInt();
        Y1 = in.nextInt();
        X2 = in.nextInt();
        Y2 = in.nextInt();
        drawCharacter = '*';
        in.nextLine();
        drawLine(X1, Y1, X2, Y2, drawCharacter);
        }
        if(firstCharacter == 'c'){
        in.nextLine();
        initGrid();
        }
        if(firstCharacter == 'd'){
        in.nextLine();
        printGrid();
        }
        if(firstCharacter == 'q'){
        return;
        }
    }
    
    }
    
    // method: plotPoint
    // purpose: receives the p command and plots the point with given coordinates and char
    public static void plotPoint(int X, int Y, char character)
    {
    grid[X][Y] = character;
    }
    
    // method: drawLine
    // purpose: receives the l command and draws a line with given coordinates and char
    public static void drawLine(int X1, int Y1, int X2, int Y2, char character)
    {
    // checks for horizontal line, left to right
    if(Y1==Y2 && X1<X2){
        for(int i = 0; i<= Math.abs(X2-X1); i++){ 
        grid[X1+i][Y1] = character;
        }
    }
    
    // checking for horizontal line, right to left
    if(Y1==Y2 && X1>X2){
        for(int i=02; i<=Math.abs(X2-X1); i++){
        grid[X2+i][Y1] = character;
        }
    }
    
    // checking for vertical line, top to bottom
    if(X1==X2 && Y1<Y2){
        for(int i = 0; i<=Math.abs(Y1-Y2); i++){
        grid[X1][Y1+i] = character;
        }
    }
    
    // checking for vertical line, bottom to top
    if(X1==X2 && Y1>Y2){
        for(int i = 0; i<=Math.abs(Y1-Y2); i++){
        grid[X1][Y2+i] = character;
        }
    } 
    
    }
    
    // method: printGrid
    // purpose: prints the grid to show the picture
    public static void printGrid()
    {
    for(int j = 0; j < grid[0].length; j++){
        for(int i = 0; i <  grid.length; i++){
        System.out.print(grid[i][j]);
        if(i == grid.length-1){
            System.out.println();
        }
    
        }
    }
    }
    }
    

    试试这个!

    【讨论】:

    • 您好,Alp,感谢您的意见。我尝试了您的修复程序,它给了我一个“java.lang.ArrayIndexOutOfBoundsException:-1”。所以我改变了周围的标志,看看会发生什么。当我这样做时,outofbounds 异常消失了,但是线条没有设置到网格中,所以我知道这绝对是我的 for 循环。不过我还是想不通。
    • 这是我的错。对不起!
    • 没问题。优秀的解决方案!我不会想到这样做(脑放屁)。为了解释 Alp 的解决方案:他将我的 for 循环的条件更改为在达到两个坐标之间的距离后停止。
    猜你喜欢
    • 1970-01-01
    • 2016-08-09
    • 2017-03-17
    • 1970-01-01
    • 1970-01-01
    • 2019-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多