Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将文件(在另一个方法中创建)调用到以后的方法中_Java - Fatal编程技术网

Java 将文件(在另一个方法中创建)调用到以后的方法中

Java 将文件(在另一个方法中创建)调用到以后的方法中,java,Java,在上面的例子中,如果第一个参数是“DEFAULT”,我在top方法(initialiseGame)中创建了一个名为boardDEFAULT的文件。现在我想在下面的方法(printBoard)中调用该文件,以便读取在top方法中建立的out.println行 import java.io.IOException; import java.io.File; import java.io.PrintWriter; import java.util.Scanner; public class Exam

在上面的例子中,如果第一个参数是“DEFAULT”,我在top方法(initialiseGame)中创建了一个名为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();
        }
    }
}
戴着一顶帽子指着黑板


我不知道如何做到这一点,我曾尝试创建一个文件数组,但这导致了更多的问题。我在方法方面有点新手,我为这个基本问题道歉。

你可能想要这样的东西:

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(...);
    }
一些阅读:


(还要注意,在您的代码
中,默认值
是一个
文件
,因此它永远不会等于
“DEFAULT”

尽管这是一种令人讨厌的方法,但您需要使用全局变量(静态)将值从一个方法传递到另一个方法

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
类实例都是全局的。

谢谢,我将查看Java文档。
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];
...