我正在尝试使用随机对象用随机生成的大写字母填充带有名为letters的引用变量的2D数组。我现在尝试在两个班级中都这样做,但我仍然遇到了一些我以前从未遇到过的错误。非常感谢任何帮助。
以下是我在WordSearch类中遇到的错误及其位置:
我收到一条错误,上面写着“char someChar =(char)(rand.nextInt(26)+ 65);” 错误内容为“令牌上的语法错误”;“,{此符号之后的预期。”
我的for循环结束时也遇到错误。 错误读取“语法错误,插入”}“以完成阻止。”
最后,我收到的错误是“公共搜索(){” 错误显示“缺少方法的返回类型。”
import java.util.Random;
import java.util.Scanner;
public class WordSearchTest {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int seed;
String word = " ";
String again = "y";
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number from 1 - 9999:\n");
seed = keyboard.nextInt();
keyboard.nextLine(); //Consume the remaining new line
while(seed < 1 || seed > 9999) {
System.out.print("You must choose a number between 1 and 9999:\n");
seed = keyboard.nextInt();
keyboard.nextLine(); //Consume the remaining new line
}
while(again.equalsIgnoreCase("y")) {
System.out.print("Choose a word to search for:\n");
word = keyboard.nextLine();
System.out.print("Would you like to search for another word? (Y = Yes and N = No)\n");
again = keyboard.nextLine();
System.out.print(again);
while(!again.equals("Y") && !again.equals("y") && !again.equals("N") && !again.equals("n")) {
System.out.print("Invalid response. Y or N?\n");
again = keyboard.nextLine();
}
}
//Random rand = new Random(seed);
//char someChar = (char)(rand.nextInt(26) + 65);
//Instantiates a WordSearch object with reference variable puzzles and passes the arguments to the WordSearch constructor
WordSearch puzzles = new WordSearch(seed, word);
puzzles.search();
System.out.print("Terminating...");
System.exit(0);
}
}
import java.util.Random;
public class WordSearch {
private int seedNum;
private String wordGiven;
private int index = 0;
private char someCharz;
char[][] letters;
private char[][] lettersFound;
public WordSearch(int seeded, String wordUser) {
seedNum = seeded;
wordGiven = wordUser;
//someCharz = charz;
}
Random rand = new Random(seedNum);
char someChar = (char)(rand.nextInt(26) + 65);
letters = new char[4][4];
lettersFound = new char[4][4];
for(int col = 0; col < letters[0].length; col++)
{
for(int rowz = 0; rowz < letters.length; rowz++)
{
System.out.print(someCharz);
}
index++;
}
public search() {
System.out.print(letters);
}
/**
* @return the seedNum
*/
public int getSeedNum() {
return seedNum;
}
/**
* @param seedNum the seedNum to set
*/
public void setSeedNum(int seedNum) {
this.seedNum = seedNum;
}
/**
* @return the wordGiven
*/
public String getWordGiven() {
return wordGiven;
}
/**
* @param wordGiven the wordGiven to set
*/
public void setWordGiven(String wordGiven) {
this.wordGiven = wordGiven;
}
}
答案 0 :(得分:1)
我希望看看我是否能找到你的第一个错误的原因,但最初,关于返回类型的错误很容易; Java中的每个方法都必须指定返回类型。如果该方法没有返回任何内容,并且说 - 在这种情况下 - 只是向控制台打印一些东西,返回类型将是$this->templateVariableContainer->add('variable', 'content');
。
将方法声明更改为void
,它将消除该错误。