Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/341.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:使用二维数组作为网格绘制_Java_Grid_Multidimensional Array - Fatal编程技术网

Java:使用二维数组作为网格绘制

Java:使用二维数组作为网格绘制,java,grid,multidimensional-array,Java,Grid,Multidimensional Array,我使用一个二维数组来表示一个使用(X,Y)坐标的网格。网格是80列乘30行。我正在使用重定向(javaproject1

我使用一个二维数组来表示一个使用(X,Y)坐标的网格。网格是80列乘30行。我正在使用重定向(javaproject1 以下是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=清除(重新初始化)

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

p=使用字符(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();
        }

        }
    }
    }

}
import java.util.Scanner;
导入java.lang.*;
公共类项目1
{
//声明名为grid的类数组
公共静态字符[][]网格=新字符[80][30];
//方法:主
//用途:调用getInput和initGrid
公共静态void main(字符串[]args)
{
initGrid();
getInput();
}
//方法:initGrid
//用途:使用空格初始化栅格,也用作清除命令
公共静态void initGrid()
{
字符填充='1';
对于(int i=0;i

关于括号,很抱歉,它没有很好地从emacs中复制出来。

您的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();
    }

    }
}
}
}
import java.util.Scanner;
导入java.lang.*;
公共类网格
{
//声明名为grid的类数组
公共静态字符[][]网格=新字符[80][30];
//方法:主
//用途:调用getInput和initGrid
公共静态void main(字符串[]args)
{
initGrid();
getInput();
}
//方法:initGrid
//用途:使用空格初始化栅格,也用作清除命令
公共静态void initGrid()
{
字符填充='1';
对于(int i=0;i如果(Y1==Y2&&X1您的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();
    }

    }
}
}
}
import java.util.Scanner;
导入java.lang.*;
公共类网格
{
//声明名为grid的类数组
公共静态字符[][]网格=新字符[80][30];
//方法:主
//用途:调用getInput和initGrid
公共静态void main(字符串[]args)
{
initGrid();
getInput();
}
//方法:initGrid
//用途:使用空格初始化网格