【问题标题】:Ladder-like word game in JavaJava中的梯形文字游戏
【发布时间】:2010-05-16 18:52:16
【问题描述】:

我发现了这个问题Choosing design method for ladder-like word game,我也想做这种程序。我写了一些代码,但已经有两个问题。这是我已经拥有的:

网格:

public class Grid {    
    public Grid(){}
    public Grid( Element e ){}
}

元素:

public class Element {  
    final int INVISIBLE = 0;
    final int EMPTY = 1;
    final int FIRST_LETTER = 2;
    final int OTHER_LETTER = 3;   
    private int state;
    private String letter;

    public Element(){}  
//empty block    
    public Element(int state){
        this("", 0);
    }  
//filled block
    public Element(String s, int state){
        this.state = state;
        this.letter = s;
    }

    public static void changeState(int s){
    }

    public int getState(){
        return state;
    }

    public boolean equalLength(){
        return true;
    }

    public boolean equalValue(){
        return true;
    }

    @Override
    public String toString(){
        return "["+letter+"]";
    }
}

主要:

import java.util.Scanner;

public class Main {
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        System.out.println("Height: ");
        while (!sc.hasNextInt()) {
            System.out.println("int, please!");
            sc.next();
        }
        final int height = sc.nextInt();
        Grid[] game = new Grid[height];

        for(int i = 1; i <= height; i++) {
            String s;
            do {
                System.out.println("Length " + i + ", please!");
                s = sc.next();
            } while (s.length() != i);

            Element[] line = new Element[s.length()+1];
            Element single = null;
            String[] temp = null;

//在这里发布

            temp = s.split("");
            System.out.println("s.length: "+s.length());
            System.out.println("temp.length: "+temp.length);

//

            for(String str : temp){
                System.out.println("str:"+str);
            }

            for (int k = 0 ; k < temp.length ; k++) {
                if( k == 0 ){
                    single = new Element(temp[k], 2);
                    System.out.println("single1: "+single);
                }
                else{
                    single = new Element(temp[k], 3);
                    System.out.println("single2: "+single);
                }
                line[k] = single;
            }

            for (Element l : line) {
                System.out.println("line:"+l);
            }

//在这里发布

            game[i] = line;
        }

//

        for (Grid g : game) {
            System.out.println(g);
        }
    }
}

以及用于调试的示例输出:

Height: 
3
Length 1, please!
A
s.length: 1
temp.length: 2
str:
str:A
single1: []
single2: [A]
line:[]
line:[A]

我认为它应该是这样工作的。我从用户那里抓住了一句话。接下来为整个游戏创建网格元素。然后为每一行创建名为 line 的 Element[] 数组。我拆分了给定的文本,这是第一个问题。为什么 string.split() 添加一个空格?您可以在输出中清楚地看到它是无缘无故添加的。我怎样才能摆脱它(现在我必须在行的长度上加上 +1 才能运行代码)。继续我将拆分的文本扔到临时字符串数组中,然后从每个字母中创建 Element 对象并将其扔到线数组中。除了这个空白的空间输出看起来很好。但下一个问题是网格。我创建了将 Element 作为参数的构造函数,但由于“不兼容的类型”,我仍然不能将行作为 Grid[] 元素抛出。我该如何解决?我做得对吗?也许我应该摆脱作为 Element[] 的 line 而只创建 Grid[][] ?

【问题讨论】:

    标签: java arrays multidimensional-array incompatibility


    【解决方案1】:

    string.split() 为什么要添加空格?

    你必须给string.split()指定一个非空的正则表达式:

    for (String t : s.split(" ")) {
        System.out.println(t);
    }
    

    【讨论】:

    • 好吧,但现在它将我的字符串拆分为 'ABC' 而不是 'A','B','C'。
    • @sasquatch90:见@psmears 答案。
    【解决方案2】:

    1 - 你可能想使用 String.charAt() 而不是 split()。

    2 - 您得到“不兼容的类型”,因为您已将“游戏”声明为“网格”对象的数组,但您尝试分配给它的“线”对象不是类型“元素数组”。使用二维数组存储元素可能会更好。

    【讨论】:

      猜你喜欢
      • 2015-06-12
      • 1970-01-01
      • 2020-03-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-26
      相关资源
      最近更新 更多