我必须创建一个java类,我可以从标准控制台读取一些命令。这就像模拟一个移动到网格中。我有一些难以创造我想要的东西。让我们说我有这个命令:
“X”和“Y”是矩阵6x6的坐标。 “DIRECTION”可以是“UP”,“DOWN”,“LEFT”,“RIGHT”。如果我写“STEP”,我会做一步。
程序应该丢弃STEP命令,直到执行了有效的START命令。之后,我可以使用STEP或其他有效的START命令,使用删除第一个的新坐标将其设置为“1”。
示例:
a)START 1,4,UP ---> OK! ANOTHER START OR STEP COMMAND
STEP ---> OK MOVE!
b)START 3,5,UP ---> OK! ANOTHER START OR STEP COMMAND
START 5,2,LEFT ---> DONE! OK NEW POSITION!
STEP ---> OK MOVE!
c)STEP ---> NO! I NEED START
OWINEVEIVNEW ---> NO! I NEED START
START 3,2,RIGHT ---> OK! ANOTHER START OR STEP COMMAND
如果我有一个START命令,我还要捕捉坐标(X,Y)和DIRECTION。
我的想法是:
public static void main(String[] args) {
Movement grid = new Movement(6); --> call constructor. 6 is dimension square grid
System.out.println("**********INSERT COMMANDS**********");
while(true){ --> loop to continue to insert commands
if (grid.startCommand()) {
System.out.println("OK START RECEIVED! I CAN USE STEP");
grid.stepForward();
} else {
System.out.println("I can't go ahead without valid START command. Try again!");
};
}
}
public boolean stepForward() {
if (start.equals("STEP") {
System.out.println("OK LET'S DO A STEP!!");
}
return true;
}
public boolean startCommand() {
Scanner sc = new Scanner(System.in);
String start = sc.next();
sc.useDelimiter("\\s+");
String[] coordinates = sc.next().split(",");
X = Integer.parseInt(coordinates[0]);
Y = Integer.parseInt(coordinates[1]);
direction = coordinates[2];
while(start.equals("START")) {
if ((0<=X && X<dim) && (0<=Y && Y<dim)){
if (direction.equals("UP")||direction.equals("DOWN")||direction.equals("LEFT")||direction.equals("RIGHT")){
cleanGrid(); --> this is just a method that put everything at 0
matrix[X][Y] = 1;
return true;
} else {
System.out.println("Your direction doesn't exist. Use \"UP\",\"DOWN\",\"LEFT\",\"RIGHT\".Try again!");
return false;
}
} else {
System.out.println("Check range of X and Y. Try again!");
return false;
}
}
System.out.println("Insert command like ex. \"START 1,2,UP\". Try again!");
return false;
}
是的,我在IF,WHILE等之间丢失了......我尝试了不同的解决方案但是我失去了插入另一个START的可能性,或者我无法识别STEP命令或其他类型的问题。 有人可以帮我解决这个问题吗?提前谢谢。
答案 0 :(得分:1)
以不同的方式考虑你的问题......
这表示您需要在执行命令后面的逻辑之前识别用户输入的命令,例如......
Scanner scanner = new Scanner(System.in);
boolean exit = false;
do {
System.out.print("CMD> ");
String input = scanner.nextLine();
if ("exit".equalsIgnoreCase(input)) {
exit = true;
} else if (input.toLowerCase().startsWith("start")) {
doStart(input);
} else if (input.toLowerCase().startsWith("step")) {
doStep(input);
}
} while (!exit);
一旦您知道用户输入的命令,就会知道执行该命令的执行方法。
然后,根据命令,您可能需要解析参数并对其进行处理......
protected void doStart(String input) {
Scanner scanner = new Scanner(input);
scanner.next(); // Command
String parameters[] = scanner.next().split(",");
int x = Integer.parseInt(parameters[0]);
int y = Integer.parseInt(parameters[1]);
String dir = parameters[2];
System.out.println(" > start @ " + x + "x" + y + " in " + dir + " direction");
}
就像你的代码说我可以写“开始2,3,向上”并继续“步骤”但我也可以像第一个命令一样写“步骤”
作为一个例子,你需要能够保持当前状态的东西......
public class BotPos {
private int x;
private int y;
private String direction;
public BotPos(int x, int y, String direction) {
this.x = x;
this.y = y;
this.direction = direction;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public String getDirection() {
return direction;
}
public void setDirection(String direction) {
this.direction = direction;
}
@Override
public String toString() {
return " > Bot is @ " + x + "x" + y + " in " + direction + " direction";
}
}
假设botPos
是一个类实例字段,那么start会设置这个状态......
protected void doStart(String input) {
Scanner scanner = new Scanner(input);
scanner.next(); // Command
String parameters[] = scanner.next().split(",");
int x = Integer.parseInt(parameters[0]);
int y = Integer.parseInt(parameters[1]);
String dir = parameters[2];
botPos = new BotPos(x, y, dir);
System.out.println(botPos);
}
doStep
会更新它,如果它可以......
protected void doStep(String input) {
if (botPos != null) {
switch (botPos.getDirection().toLowerCase()) {
case "up":
botPos.setY(botPos.getY() - 1);
break;
case "down":
botPos.setY(botPos.getY() + 1);
break;
case "left":
botPos.setX(botPos.getX() - 1);
break;
case "right":
botPos.setX(botPos.getX() + 1);
break;
}
System.out.println(botPos);
} else {
System.out.println(" > Invalid state, you have no start position");
}
}
现在,你也可以将botPos
传递给方法,但想法是一样的。
如果需要,您只需将botPos
设置为null
即可使其无效