我将如何读取txt文件并将其输出到java中的控制台?

问题描述

我想将 level1.txt 文件输出到控制台,但将其存储在一个字符数组中,以便我可以在输出时更改某些图标的颜色,因此 # 将是棕色的例如,我的问题是我什至无法从扫描仪输出字符串,我可以实施任何提示或修复吗?

另外,我希望能够实现一个 xy 轴让玩家在 txt 地图上移动,但我不知道如何实现

这是 game.java代码

import java.io.FileNotFoundException;
import java.io.File;
import java.util.Scanner;
public class game {
    // change weapon to have its own file for certain weapons,// make at least 5 different ones with different atributes
    public static String weapon = "fists";
    public static Scanner s = new Scanner(system.in);
    public static char levelLoader[] = new char[100000];
    public static String level = "";
    public static File file = new File("level1.txt");

    private static void healthBar() {
        System.out.println(color.GREEN_BOLD_BRIGHT + "Health: " + color.RESET +
            color.RED + player.maxHealth + "/" + player.health +
            color.RESET + color.GREEN_BOLD_BRIGHT + " XP: " + color.RESET +
            color.YELLOW + player.maxXp + "/" + player.xp +
            color.RESET + color.GREEN_BOLD_BRIGHT + " Weapon: " + color.RESET +
            color.CYAN + weapon + color.RESET);
    }

    // starts up game and tells player how to move
    public static void intro() {
        try {startUp.clear();} catch(Exception e) {}
        System.out.println("You can move by either pressing W,A,S,D or " +
                "type 'exit' to close the game,your character is the @ ");
        startUp.slow();
        try {startUp.clear();} catch(Exception e) {}
        player.initializePlayerAtributes();
    }

    public static void paitWalls() {
    }

    //prints game board to terminal
    public static void paintGameBoard() throws FileNotFoundException {
        healthBar();

        Scanner levelReader = new Scanner(file);
        level = levelReader.nextLine();
        while(levelReader.hasNextLine()) {
            level = level + "\n" + levelReader.nextLine();
        }
        levelLoader = level.tochararray();
        for (int i = 0; i > levelLoader.length; i++) {
            System.out.println(levelLoader[i]);
        }
        levelReader.close();
    }

    // uses w a s d and exit to determine if to move or exit
    public static void playerMovementAndExit() {
        String movementOrPause = s.nextLine();
        switch (movementOrPause.toLowerCase()) {
            case "w":
                player.y++;
                try {startUp.clear();} catch(Exception e) {}
                break;
            case "s":
                player.y--;
                try {startUp.clear();} catch(Exception e) {}
                break;
            case "d":
                player.x++;
                try {startUp.clear();} catch(Exception e) {}
                break;
            case "a":
                player.x--;
                try {startUp.clear();} catch(Exception e) {}
                break;
            case "exit":
                try {startUp.clear();} catch(Exception e) {}
                System.exit(1);
                break;
            default:
                System.out.println("You need to move with W A S D or" +
                        " type 'exit' to close");
                startUp.slow();
                try {startUp.clear();} catch(Exception e) { }
                break;
        }
    }

    public static void run() {
        intro();
        while(true){
            try {paintGameBoard();} catch (Exception e) {}
            playerMovementAndExit();
        }
    }
}

这是 level1.txt 文件

#########################################
#.........#.........#...................#
#...................#...................#
#.........#.........#...................#
###############...###...................#
#.........#.........#...................#
#.......................................#
#.........#.........#...................#
#########################################

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)