【问题标题】:Passing commands to program as a text file将命令作为文本文件传递给程序
【发布时间】: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/…

标签: java linux terminal


【解决方案1】:

通常,当您要为没有的程序传递配置文件时,请使用“

尽量不带“

java ConsoleRobot commands_in_english.txt

【讨论】:

  • 它给了我同样的错误; Error: Could not find or load main class ConsoleRobot
  • 我也试过java -cp .:robotworld.jar ConsoleRobot &lt; commands_in_english.txtjava -cp .:robotworld.jar ConsoleRobot commands_in_english.txt。前者运行程序但我提供的指令没有通过,后者也运行程序但它正常运行,提示我通过终端输入指令。
猜你喜欢
  • 2016-06-20
  • 2015-08-13
  • 1970-01-01
  • 2023-04-04
  • 2010-11-06
  • 1970-01-01
  • 2012-12-21
  • 2021-11-20
  • 2019-03-23
相关资源
最近更新 更多