【问题标题】:Java text game parser problemsJava文本游戏解析器问题
【发布时间】:2013-02-20 13:58:27
【问题描述】:

这是我的第一篇文章,我只是 Java 新手,如果不符合要求,请见谅。

我一直在用 Java 编写一个基于文本的冒险游戏,但我的代码在一个地方失败了——解析器。没有错误,它只是不起作用。它接受输入但不做任何事情。它非常简单,看起来像这样:

public static void getInput(){
    System.out.print(">>"); //print cue for input
    String i = scan.nextLine(); //get (i)nput
    String[] w = i.split(" "); //split input into (w)ords
    List words = Arrays.asList(w); //change to list format
    test(words);
}

测试方法只是使用if(words.contains("<word>"))在列表中搜索某些单词。

代码有什么问题,我该如何改进?

【问题讨论】:

  • 错误信息是什么?
  • 编译器在说什么?还是运行时错误?请告诉我们到底发生了什么
  • “在一个地方让我失望了”它是怎么失败的?
  • 没有错误,只是不起作用。它接受输入,但什么也不做。
  • 尝试使用断点调试代码

标签: java text adventure


【解决方案1】:

如何保留数组并使用类似的东西:

    String[] word_list = {"This","is","an","Array"}; //An Array in your fault its 'w'
for (int i = 0;i < word_list.length;i++) { //Running trough all Elements 
    System.out.println(word_list[i]);
            if (word_list[i].equalsIgnoreCase("This")) {
        System.out.println("This found!");
    }
    if (word_list[i].equalsIgnoreCase("is")) {
        System.out.println("is found!");
    }
    if (word_list[i].equalsIgnoreCase("an")) {
        System.out.println("an found!");
    }
    if (word_list[i].equalsIgnoreCase("Array")) {
        System.out.println("Array found!");
    }
    if (word_list[i].equalsIgnoreCase("NotExistant")) { //Wont be found
        System.out.println("NotExistant found!"); 
    }
}

你会得到以下输出:

This found!
is found!
an found!
Array found!

如您所见,您根本不需要将其转换为列表!

【讨论】:

  • 我喜欢这个。我只想要一个基本的两词解析器,所以如果我使用 word_list[0] 而不是 for 循环和 [i] 会起作用吗?
  • 嗯,我试过了,但没用。我认为问题可能在于拆分它。
  • 我放了一个 System.out.println 来打印出单词数组,它想出了 [Ljava.lang.String;@3f84246a 帮帮我!
  • 尝试使用它来获取 word_list: String[] word_list = System.console().readLine().split(" ");
  • 它出现了 NullPointerException
【解决方案2】:

我会这样做:

public class ReadInput {
    private static void processInput (List <String> words)
    {
        if (words.contains ("foo"))
            System.out.println ("Foo!");
        else if (words.contains ("bar"))
            System.out.println ("Bar!");
    }

    public static void readInput () throws Exception
    {
        BufferedReader reader = 
            new BufferedReader (
                new InputStreamReader (System.in));

        String line;
        while ((line = reader.readLine ()) != null)
        {
            String [] words = line.split (" ");
            processInput (Arrays.asList (words));
        }
    }

    public static void main (String [] args) throws Exception 
    {
        readInput ();
    }
}

示例会话:

[in]  hello world
[in]  foo bar
[out] Foo!
[in]  foobar bar foobar
[out] Bar!

【讨论】:

  • @SamBrev System.in 只是 Java 中的标准输入。 InputStreamReader 是一个Reader,它从给定的InputStream 中读取字符。 Java 中的Reader 类似于InputStream,但读取的是unicode 字符而不是字节。 BufferedReader 包装 InputStreamReader 并添加逐行读取输入的功能。其余的都应该是显而易见的。
  • 抛出异常;我在哪里捕捉它以及如何捕捉它?
  • @SamBrev 这取决于它抛出什么异常以及在哪里。
  • @SamBrev throws Exception 子句意味着该方法可能会抛出异常。从 STDIN 读取时这不太可能,所以不要介意。方法main被JVM调用,所以JVM负责处理它抛出的任何异常。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多