学校 Java 逃离房间程序

huangapple go评论60阅读模式
英文:

School Java Escape Room Program

问题

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;

public class EscapeRoom {

    ArrayList<String> Messages;
    String fileName;
    private boolean win = false;

    public void readFile() throws FileNotFoundException {

        Messages = new ArrayList<String>();
        fileName = "test.txt";
        
        File file = new File(fileName);    
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine()){
            Messages.add(scanner.nextLine());
        }
        scanner.close();

    }    
    
    public void showGreeting(){
        System.out.println(Messages.get(0));
    }
    public void showDirections(){
        System.out.println(Messages.get(1));
    }
    public void showWin() {
        System.out.println(Messages.get(2));
    }
    public void showLoss() {
        System.out.println(Messages.get(3));
    }
}
英文:

I am writing an escape room project for school which has messages such as a greeting, a win message, a loss message, etc. I stored these messages in a text file and then I read the file and store each line into one ArrayList and to access each line by their respective getter method and I use the .get function with their index value. I'm wondering if there is a way to avoid hardcoding the index numbers and on top of that is there a way I can just read the file when the program is run instead of having to make an instance of the class and then for example doing foo.readFile();

import java.io.File;
import java.util.Scanner;
import java.io.FileNotFoundException;
import java.util.ArrayList;
//package Stage;

public class EscapeRoom{

	ArrayList&lt;String&gt; Messages;
	String fileName;
	private boolean win = false;

	public void readFile() throws FileNotFoundException {

		Messages = new ArrayList&lt;String&gt;();
		fileName = &quot;test.txt&quot;;
		
		File file = new File(fileName);	
		Scanner scanner = new Scanner(file);
		while(scanner.hasNextLine()){
			Messages.add(scanner.nextLine());
		}
		scanner.close();

	}	
	
	public void showGreeting(){
		System.out.println(Messages.get(0));
	}
	public void showDirections(){
		System.out.println(Messages.get(1));
	}
	public void showWin() {
		System.out.println(Messages.get(2));
	}
	public void showLoss() {
		System.out.println(Messages.get(3));
	}
}

答案1

得分: 2

这正是属性文件的用途。这里是我命名为 prompts.properties 的文件内容:

greeting = "你好,欢迎来到我的游戏"
win = "你赢了,耶!"
loss = "抱歉,兄弟,你输了"
directions = "逃命吧!"

以下是您修改后的程序代码:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class EscapeRoom {

    Properties prompts = null;

    public void readFile() throws IOException {
        prompts = new Properties();
        prompts.load(new FileInputStream("prompts.properties"));
    }

    public void showGreeting() {
        System.out.println(prompts.get("greeting"));
    }

    public void showDirections() {
        System.out.println(prompts.get("directions"));
    }

    public void showWin() {
        System.out.println(prompts.get("win"));
    }

    public void showLoss() {
        System.out.println(prompts.get("loss"));
    }
}

基本上,要获取一个命名的键值对,您需要类似于映射的东西。属性实际上是一种特殊类型的映射,它理解具有格式 <key> = <value> 的记录文件。

这是关于 Properties 的文档,如果您决定自己实现,可以使用 Map 来实现相同的基本功能。

英文:

This is exactly what a properties file is for. Here is a file I named prompts.properties:

greeting = &quot;hello, welcome to my game&quot;
win = &quot;you win, yay&quot;
loss = &quot;sorry bro, you lose&quot;
directions = &quot;escape with your life!&quot;

Here is your modified program:

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

public class EscapeRoom {

    Properties prompts = null;

    public void readFile() throws IOException {
        prompts = new Properties();
        prompts.load(new FileInputStream(&quot;prompts.properties&quot;));
    }

    public void showGreeting() {
        System.out.println(prompts.get(&quot;greeting&quot;));
    }

    public void showDirections() {
        System.out.println(prompts.get(&quot;directions&quot;));
    }

    public void showWin() {
        System.out.println(prompts.get(&quot;win&quot;));
    }

    public void showLoss() {
        System.out.println(prompts.get(&quot;loss&quot;));
    }
}

Basically to get a named key-value pair you want something like a map. Properties are really a special sort of map that understands a file of records that have the format &lt;key&gt; = &lt;value&gt;.

Here is the documentation on Properties and if you decide to roll your own you would implement the same basic thing with a Map.

huangapple
  • 本文由 发表于 2020年9月26日 01:14:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/64068608.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定