gpt4 book ai didi

java - 如何使 args 函数等于 2?因为它只是抛出文件未找到异常

转载 作者:行者123 更新时间:2023-12-02 08:49:53 26 4
gpt4 key购买 nike

/**
* Compilation : javac Traversal.java
* Execution : java Traversal args0 ----- GraphicsMode
* : java Traversal args0 args1 ----- TextMode
*
* Arguments : args0 - boardfilename.txt
* args1 - movesfilename.txt
*
* Controls : h - left movement
* l - right movement
* j - down movement
* k - up movement
**/
public static void main(String[] args) throws FileNotFoundException, InterruptedException {
if (args.length == 1) {
GameBoard board = Middleware.parseArgs(args[0]);
Middleware.validateBoard(board);
GraphicsMode.play(board);
}else if (args.length == 2) {
GameBoard board = Middleware.parseArgs(args[0]);
Middleware.validateBoard(board);
String moves = Middleware.readMoves(new Scanner(new File(args[1])));
TextMode.play(board, moves);
}
exit();
}

/**
* Sleep for 4000 microseconds to allow
* the final event sound to finish
* playing and then exits.
*/
public static void exit() throws InterruptedException {
Thread.sleep(4000);
System.exit(0);
}

最佳答案

正如 JavaDoc 中已经提到的,如果您希望将两个参数传递给程序,则需要从命令提示符(cmd 窗口)运行该程序,如下所示:

java Traversal args0 args1

哪里

args0 - boardfilename.txt                  
args1 - movesfilename.txt

换句话说,将其运行为 java Traversal boardfilename.txt movesfilename.txt从命令提示符。如果boardfilename.txtmovesfilename.txt不在 Traversal.class 的同一位置是,提及他们的完整路径,即

java Traversal path-to-boardfilename.txt path-to-movesfilename.txt

如果您在 Eclipse 中运行它,请右键单击 Traversal.java> 单击运行配置...> 将值放入对话框中,如下所示屏幕截图 > 单击运行

enter image description here

在这种情况下(即当您尝试从 Eclipse 运行它时),请确保 boardfilename.txtmovesfilename.txt是否在 src 的父文件夹中.

关于java - 如何使 args 函数等于 2?因为它只是抛出文件未找到异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60849166/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com