在上面的例子中,在top方法(initialiseGame)中,如果第一个参数是“DEFAULT”,我创建了一个名为boardDEFAULT的文件。现在我想在下面的方法(printBoard)中调用该文件,以便读取top方法中建立的out.println行。
import java.io.IOException;
import java.io.File;
import java.io.PrintWriter;
import java.util.Scanner;
public class Example {
public static void initialiseGame(String configFileName, String...args) throws IOException {
configFileName = args[0];
if ("DEFAULT".equals(configFileName)) {
try {
File boardDEFAULT = new File("easy_board.txt");
PrintWriter output = new PrintWriter(boardDEFAULT);
output.println("######");
output.println("#@ &2#");
output.println("## ##");
output.close();
} catch (IOException ex) {
System.out.println("Error");
return; }
}
}
public static void printBoard() {
if ("DEFAULT".equals(boardDEFAULT)) {
String [][] DefaultBoardArray = new String [6][3];
DefaultBoardArray[0][0] = "#";
DefaultBoardArray[1][0] = "#";
DefaultBoardArray[2][0] = "#";
DefaultBoardArray[3][0] = "#";
DefaultBoardArray[4][0] = "#";
DefaultBoardArray[5][0] = "#";
DefaultBoardArray[0][1] = "#";
DefaultBoardArray[1][1] = "@";
DefaultBoardArray[2][1] = " ";
DefaultBoardArray[3][1] = "&";
DefaultBoardArray[4][1] = "2";
DefaultBoardArray[5][1] = "#";
DefaultBoardArray[0][2] = "#";
DefaultBoardArray[1][2] = "#";
DefaultBoardArray[2][2] = " ";
DefaultBoardArray[3][2] = "#";
DefaultBoardArray[4][2] = "#";
DefaultBoardArray[5][2] = "#";
for (int i=0; i<7; i++) {
for (int j=0; j<3; j++) {
System.out.print(DefaultBoardArray[i][j] + " ");
}
}
}
}
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Error: Too few arguments given. Expected 1 argument, found "+args.length+".");
System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
return;
}
if (args.length >1) {
System.out.println("Error: Too many arguments given. Expected 1 argument, found "+args.length+".");
System.out.println("Usage: MazeGame [<game configuration file>|DEFAULT]");
return;
}
Scanner keyboard = new Scanner (System.in);
String [] Command = new String [2];
Command[0] = keyboard.nextLine();
while (!("help".equalsIgnoreCase(Command[0])) || !("board".equalsIgnoreCase(Command[0])) || !("status".equalsIgnoreCase(Command[0])) || !("left".equalsIgnoreCase(Command[0])) || !("right".equalsIgnoreCase(Command[0])) || !("up".equalsIgnoreCase(Command[0])) || !("down".equalsIgnoreCase(Command[0])) || !("save <file>".equalsIgnoreCase(Command[0]))) {
if (!("help".equalsIgnoreCase(Command[1])) || !("board".equalsIgnoreCase(Command[1])) || !("status".equalsIgnoreCase(Command[1])) || !("left".equalsIgnoreCase(Command[1])) || !("right".equalsIgnoreCase(Command[1])) || !("up".equalsIgnoreCase(Command[1])) || !("down".equalsIgnoreCase(Command[1])) || !("save <file>".equalsIgnoreCase(Command[1]))) {
System.out.println("Error: Could not file command '"+Command[1]+"'.");
} else {
System.out.println("Error: Could not file command '"+Command[0]+"'."); }
System.out.println("To find the list of valid commands, please type 'help'.");
Command[1] = keyboard.nextLine();
if (("help".equalsIgnoreCase(Command[1])) || ("board".equalsIgnoreCase(Command[1])) || ("status".equalsIgnoreCase(Command[1])) || ("left".equalsIgnoreCase(Command[1])) || ("right".equalsIgnoreCase(Command[1])) || ("up".equalsIgnoreCase(Command[1])) || ("down".equalsIgnoreCase(Command[1])) || ("save <file>".equalsIgnoreCase(Command[1]))) {
break;
}
}
if (("board".equalsIgnoreCase(Command[0])) || ("board".equalsIgnoreCase(Command[1]))) {
printBoard();
}
}
}
反过来我收到错误
error: cannot find symbol - if ("DEFAULT".equals(boardDEFAULT))
用帽子指着boardDEFAULT。
我不知道如何实现这一点我尝试创建一个文件数组,但这导致了更多问题。我对方法有点新手,为这个基本问题道歉。
答案 0 :(得分:1)
你可能想要这样的东西:
public class Example {
private boolean isDefault = false; // class member => available in all non static methods
public void initialiseGame(String configFileName, String...args) throws IOException {
configFileName = args[0]; // NB: this makes no sense
if ("DEFAULT".equals(configFileName)) {
isDefault = true;
// ...
public void printBoard() {
if (this.isDefault) {
// ...
public static void main(String[] args) {
Example example = new Example();
// use example: example.initialiseGame(...);
}
一些阅读:https://docs.oracle.com/javase/tutorial/java/javaOO/variables.html
(另请注意,代码boardDEFAULT
中的File
是"DEFAULT"
所以它永远不会等于MvxActivity<MyViewModel>
)
答案 1 :(得分:0)
虽然这是一种令人讨厌的方式,但您需要使用全局变量(静态)将值从一个方法传递到另一个方法。
public class Example {
private static isDefualtConfig = false; // value shared by both methods
public static void initialiseGame(String configFileName, String...args) throws IOException {
configFileName = args[0];
if ("DEFAULT".equals(configFileName)) {
isDefualtConfig =true;
...
}
public static void printBoard() {
if (isDefualtConfig) {
String [][] DefaultBoardArray = new String [6][3];
...
通常,静态方法难以使用。您的示例无法编译,因为来自一个静态方法的参数在另一个静态方法中不可见。所以你需要创建一个标志,它将被用作它们之间的缓存。
这有一个缺点。您的initialiseGame
方法对于所有Example
类实例都是全局的。