我目前正在尝试用 Java 构建一个绘图工具,使用终端中的命令提示符控制笔。
到目前为止,我已经能够执行命令来告诉笔移动、转动等。现在,我试图让我的程序响应颜色命令,然后我尝试放入扫描仪来读取下一个单词. 在我包含的代码示例中,我尝试让扫描仪检测“红色”一词,然后更改笔颜色(该方法保存在“笔”类中)。
所以,理想情况下,我想在终端输入:
colour red
...并获得将笔的颜色更改为红色的结果操作。
再次寻求您可以提前提供的任何帮助。如果您在提供帮助之前还需要了解其他任何信息,请告诉我!
/**
* Allow the user to draw on the canvas by typing commands.
*/
public void draw()
{
boolean finished = false;
printWelcome();
printPenLocation();
while(!finished) {
LinkedList<String> command = reader.getInput();
if(!command.isEmpty()) {
String firstWord = command.get(0);
switch(firstWord) {
//Had more examples of case commands here, such as "move", "help", etc ...
case "colour":
Scanner scannerC = new Scanner(System.in);
String colour = scannerC.nextLine();
if (scannerC.nextLine().equalsIgnoreCase("red"))
{
pen.setColor(Color.RED);
}
else {
System.out.println("Unrecognised colour!");
}
break;
default:
System.out.println("Unrecognised command: " + firstWord);
break;
}
}
}
}
生成的代码可以正确编译并且不会引发错误,但不会改变笔的颜色!如果有一双专家的眼睛来批评我的代码,我将不胜感激。