如何将输入文本文件转换为单个变量?JAVA
我需要帮助为我的计算机科学课制作一个小型火星着陆器电子游戏。我们必须使用扫描仪读取游戏配置文本文件,并将其用作游戏不同方面的规则(重力、燃油量等)。她给了我们不同的文本文件,它们都有不同的难度和值,但它们都有相同的格式,因此,我需要能够简单地调用不同的文本文件,并有一个新的水平准备发挥。我的问题是: 如何将文件中的输入输入到单独的变量中,以便我可以操纵它们来创建游戏 下面是读取文本文件的代码,它还将文本文件打印到控制台如何将输入文本文件转换为单个变量?JAVA,java,Java,我需要帮助为我的计算机科学课制作一个小型火星着陆器电子游戏。我们必须使用扫描仪读取游戏配置文本文件,并将其用作游戏不同方面的规则(重力、燃油量等)。她给了我们不同的文本文件,它们都有不同的难度和值,但它们都有相同的格式,因此,我需要能够简单地调用不同的文本文件,并有一个新的水平准备发挥。我的问题是: 如何将文件中的输入输入到单独的变量中,以便我可以操纵它们来创建游戏 下面是读取文本文件的代码,它还将文本文件打印到控制台 import java.io.File; import java.io.Fi
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class MarsLander {
public static void main(String [] args) {
try {
Scanner sc = new Scanner(new File("gameConfig.txt"));
while (sc.hasNext()){
String s = sc.next();
System.out.println(s);
}
sc.close();
}
catch (FileNotFoundException e) {
System.out.println("Failed to open file!");
}
}
}
以下是其中一个文本游戏配置文件:
1000 500
mars_sky.jpg
ship.png ship_bottom.png ship_left.png ship_right.png ship_landed.png ship_crashed.png
20 50
500.0 400.0
100
thrust.wav yay.wav explosion.wav
-0.1
2.0
0.5
500 50
在我的解决方案中,我考虑了一些更一般的东西。首先让我向您展示我编写的代码。然后,我将解释其行为和特殊性
public class GameExample {
private static class Game {
private Long x, y;
private List<String> images = new ArrayList<>();
private Game(final Long x, final Long y, final List<String> images) {
this.x = x;
this.y = y;
this.images = images;
}
public Long getX() {
return x;
}
public Long getY() {
return y;
}
public List<String> getImages() {
return images;
}
public static class Builder {
// Parsing methods used by the builder to read the files and build the configuration
// TODO: add here builder methods for each line of the file
private final List<BiFunction<String, Game.Builder, Game.Builder>> parsingMethods = Arrays.asList(
(str, builder) -> builder.withPositions(str),
(str, builder) -> builder.withImages(str));
private Long x, y;
private List<String> images = new ArrayList<>();
private Builder withPositions(final String str) {
String[] positions = str.split(" ");
x = Long.valueOf(positions[0]);
y = Long.valueOf(positions[1]);
return this;
}
private Builder withImages(final String str) {
Stream.of(str.split(" ")).forEach(imgStr -> images.add(imgStr));
return this;
}
public Game build(final String filename) throws IOException {
Scanner sc = null;
try {
// Read the file line by line
List<String> lines = Files.lines(Paths.get(filename)).collect(Collectors.toList());
// Iterate over each line and call the configured method
IntStream.range(0, lines.size()).forEach(
index -> parsingMethods.get(index)
.apply(lines.get(index), this));
// Build an instance of the game
return new Game(x, y, images);
} catch (IOException e) {
e.printStackTrace();
throw e;
} finally {
if (sc != null) sc.close();
}
}
}
}
public static void main(String[] args) throws IOException {
final Game.Builder builder = new Game.Builder();
Game game = builder.build("file.txt");
System.out.println(game.getX() + ":" + game.getY());
System.out.println(game.getImages());
}
}
这张单子上写着:
build
方法中,它实现了在以下行上执行方法的逻辑:
// Iterate over each line and call the configured method
IntStream.range(0, lines.size()).forEach(
index -> parsingMethods.get(index)
.apply(lines.get(index), this));
因此,我们可以通过执行以下操作轻松解析新行数据:
parsingMethods
的列表中如果它们总是以相同的格式和长度显示。您不需要循环,而是每个
sc.next()
都是不同的变量。一定要记得对varaible类型使用正确的next。啊,这就是我需要知道的,谢谢!您不应该将这些属性从配置文件转换为变量,而应该使用允许您存储这些值并在以后引用它们的方法,例如:config.get(“some_key”)
作为一般规则,您应该尽可能地将问题具体化,否则看起来像是“请为我做家庭作业”您可以使用例如属性文件作为配置文件-查看那边,或者您可以使用一些配置类,使用方法getEasyConfig()
getHardConfig()
等,而不是使用配置属性的名称,例如在enum中,在config中,您可以返回带有该参数的映射,或者任何内容:)
// Iterate over each line and call the configured method
IntStream.range(0, lines.size()).forEach(
index -> parsingMethods.get(index)
.apply(lines.get(index), this));