首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >纵横字母码错误

纵横字母码错误
EN

Stack Overflow用户
提问于 2011-11-21 06:18:16
回答 2查看 662关注 0票数 0

我正在用Java做一个填字游戏程序,我被卡住了。

每当我尝试执行像java assign2 > input.txt这样的代码时,什么都没有发生,它就像一个无限循环。

假设我的纵横填字游戏程序不完整,只是如果我不能测试它,我就不能做其他任何事情,如果你能帮助我,这是我的代码。

代码语言:javascript
复制
import java.util.*;

public class A2
{
    public static void main(String[] args)
    {
        String[] a = new String[100];
        Scanner scanner = new Scanner(System.in);
        String t = scanner.nextLine();
        Crossword cw = new Crossword(t);
        int count = 0;
        System.out.print("1");
        for (; !t.equals(""); count++)
        {
            System.out.print("2");
            a[count] = t;
            t = scanner.nextLine();
        }
        for (int j = 0; j < 20; j++)
        {
            for (int k = 0; k < 20; k++)
                System.out.print(cw.crossword[j][k]);
            System.out.println("");
        }
    }
}

/**
   The class Crossword knows how to build a crossword layout from
   a list of words.
*/
class Crossword
{
    public char[][] crossword = new char[20][20];

    public Crossword(String first)
    {
        for (int i = 0; i < first.length(); i++)
            crossword[9][i] = first.charAt(i);
    }
}

在这一点上我要放弃了,所以任何帮助都将不胜感激。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-21 06:21:08

看起来您正在向您正在读取的同一文件中写入数据。您从"input.txt"读取数据,然后使用java assign2 > input.txt (或java A2...)调用您的程序。

这意味着,当您写入被重定向到input.txtSystem.out时,文件将获得更多的行可供读取,并且您的条件!t.equals("")永远不会变为false。

票数 1
EN

Stack Overflow用户

发布于 2011-11-23 05:59:33

我已经给了它一个旋转,它似乎确实工作-但是,检查你的文件重定向的方向;我想你是在追求:

代码语言:javascript
复制
java assign2 < input.txt

目前还不完全清楚您试图在输出方面实现什么目标,但我怀疑您需要更接近以下内容:

代码语言:javascript
复制
public class Main {
    private static Crossword[] crosswords = new Crossword[20];

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

        String line = scanner.nextLine();
        int index = 0;

        while (!line.equals("") && index < 20) {
            crosswords[index++] = new Crossword(line);
            line = scanner.nextLine();
        }

        for (int i=0; i < 20; i++) {
            for (int j=0; j < 20; j++) {
                if (crosswords[i] != null) {
                    System.out.print(crosswords[i].crossword[j]);                
                } else {
                    System.out.print("");                
                }
            }
            System.out.println("");
        }
    }
}

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8205202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档