【发布时间】:2015-11-01 17:19:10
【问题描述】:
我是 Java 新手。我正在开发一个 Java 控制台应用程序,其中有一个字段和一个在其中跳跃的青蛙,用户通过二维数组中的输入来决定字段的大小,(有点像棋盘,但不同之处在于用户决定如何领域应该很大)。例如,用户输入以英尺为单位的字段高度和以英尺为单位的宽度。到目前为止,我已经设法完成了一些 Field 类和 Position 类,它们从用户那里获取输入并将其放在一个数组中(int [][]fieldsize)。
类控制器:
package project;
public class Controller {
public static void main( String[] args ) {
Field field = new Field();
Position position = new Position();
}
}
类字段:
package project;
import java.util.Scanner;
public class Field{
int y;
int x;
int[][] fieldsize;
public Field() {
Scanner scan = new Scanner(System.in);
System.out
.println("Enter the size of the field in feets(width
and length separated by space, x y):");
x = scan.nextInt();
y = scan.nextInt();
int[][] fieldsize = new int[y][x];
this.fieldsize= fieldsize;
}
public int[][] getFieldSize() {
return fieldsize;
}
}
我已经设法将用户输入的字段大小“收集”到 int [][] 字段大小数组中。
现在我想询问用户青蛙的起始位置和前进方向,S(南),N(北),E(东),W(西),然后将青蛙添加到字段中。例如,用户键入 3 4 E。这应该将青蛙放在位置 [3] [4] East(Heading)。我该如何解决这个问题?
班级位置:
package project;
import java.util.Scanner;
public class Position {
public Position() {
int x;
int y;
String heading;
Scanner scan = new Scanner(System.in);
System.out.println("Enter the starting position and heading for the frog, X Y and N, S, W or E position");
x = scan.nextInt();
y = scan.nextInt();
heading = scan.next();
//How do I put this inputs in the Field(fieldsize)?? So they get into this position in the field??
}
}
我的意思有点难以描述,但我希望你们能帮助我! 提前致谢!
【问题讨论】: