我试图将命令作为文本文件传递给我的程序。我创建了一个名为 commands_in_english.txt 的文件,并尝试传递说明,但它给了我一条错误消息。不确定我是否错误地传入了文件,或者我是否在文本文件中遗漏了某些内容。任何帮助将不胜感激。
命令应作为文件传递给程序,每行一条指令。英语说明位于 the files commands_in_english.txt 中。我按照以下说明传递了该程序:
java ConsoleRobot < commands_in_english.txt
这是我尝试将命令传递给的程序:
import java.util.Scanner;
public class ConsoleRobot extends SmarterRobot
{
public static void main(String [] args)
{
World yard = new World();
SmarterRobot ringo = new SmarterRobot();
yard.add(ringo,5,4);
yard.addBeeper(5,9);
yard.addBeeper(4,5);
yard.addBeeper(9,4);
yard.addBeeper(9,5);
Scanner scan = new Scanner(System.in);
System.out.println("Enter a command: | Introduzca un comando:");
String command = scan.nextLine();
command = command.toLowerCase();
while (!command.equals("stop") && !command.equals("detener"))
{
System.out.println("command = "+command);
if ( command.equals("forward") || command.equals("adelante"))
{
Scanner reader = new Scanner(System.in);
System.out.println("How far should the robot move?");
int input = reader.nextInt();
ringo.moveNumOfTimes(input);
}
else if ( command.equals("right") || command.equals("derecha"))
{
ringo.turnRight();
}
else if ( command.equals("left") || command.equals("izquierda"))
{
ringo.turnLeft();
}
else if ( command.equals("collect") || command.equals("recoger"))
{
ringo.pickBeeper();
}
else if ( command.equals("drop") || command.equals("soltar"))
{
ringo.putBeeper();
}
else
{
System.out.println("I don't understand! | No entiendo!");
}
System.out.println("Enter a command: | Introduzca un comando:");
command = scan.nextLine();
}
System.out.println("Finished | Terminado");
}
}
答案 0 :(得分:0)
Normaly,当你想传递一个程序的配置文件时,你没有使用“&lt;”。
尝试不通过“&lt;”
传递java ConsoleRobot commands_in_english.txt