我正在用Java做一个填字游戏程序,我被卡住了。
每当我尝试执行像java assign2 > input.txt这样的代码时,什么都没有发生,它就像一个无限循环。
假设我的纵横填字游戏程序不完整,只是如果我不能测试它,我就不能做其他任何事情,如果你能帮助我,这是我的代码。
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);
}
}在这一点上我要放弃了,所以任何帮助都将不胜感激。
发布于 2011-11-21 06:21:08
看起来您正在向您正在读取的同一文件中写入数据。您从"input.txt"读取数据,然后使用java assign2 > input.txt (或java A2...)调用您的程序。
这意味着,当您写入被重定向到input.txt的System.out时,文件将获得更多的行可供读取,并且您的条件!t.equals("")永远不会变为false。
发布于 2011-11-23 05:59:33
我已经给了它一个旋转,它似乎确实工作-但是,检查你的文件重定向的方向;我想你是在追求:
java assign2 < input.txt目前还不完全清楚您试图在输出方面实现什么目标,但我怀疑您需要更接近以下内容:
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("");
}
}
}希望这能有所帮助。
https://stackoverflow.com/questions/8205202
复制相似问题