【发布时间】:2016-02-24 10:57:20
【问题描述】:
我正在尝试将命令作为文本文件传递给我的程序。我创建了一个名为 commands_in_english.txt 的文件,并尝试传入说明,但它给了我一条错误消息。不确定我是否传递了错误的文件,或者我的文本文件中是否缺少某些内容。任何帮助将不胜感激。
命令应该作为文件传递给程序,每行一条指令。 文件 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");
}
}
【问题讨论】:
-
你能否展示你的代码库来理解你试图实现的整体目标
-
您当前的语法尝试将文件内容传递给应用程序的输入流 (
System.in),而不是作为main方法的参数。 -
你不能将文件作为参数传递给 Java 程序,但你可以传递它的路径...检查stackoverflow.com/questions/4302567/…