【发布时间】:2017-10-04 21:38:56
【问题描述】:
我正在尝试使用 Random 对象使用名为 letters 的参考变量填充 2D 数组,其中使用随机生成的大写字母。我现在已经尝试在两个课程中都这样做了,但我仍然遇到了一些我以前从未遇到过的错误。非常感谢您对此提供任何帮助。
以下是我在 WordSearch 类中遇到的错误及其所在位置:
我收到一条错误消息:“char someChar = (char)(rand.nextInt(26) + 65);” 错误内容为“令牌“;”上的语法错误,{ 应在此令牌之后。”
在我的 for 循环结束时 } 所在的位置也出现错误。 错误内容为“语法错误,插入“}”以完成块。”
最后,我在“public search(){”这一行收到一条错误消息 错误显示为“方法的返回类型丢失。”
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;
}
}
【问题讨论】:
-
您不能在方法之外编写代码,并且您的
WordSearch类有一堆方法之外的代码。 -
在方法或构造函数之外,您有一整段代码(在您的
WordSearch类中以Random rand = new Random(seedNum);开头,还有几行)。那不会编译。 -
“方法的返回类型丢失” - 那么,该方法的返回类型应该是什么?你指定了吗?该消息非常不言自明
-
感谢 csm_dev 和 Juan Carlos Mendoza 解决了我遇到的前两个错误!
-
嗨 Erwin Bowidt,我尝试将 void 作为返回类型,它清除了方法标头中的错误,但现在当我尝试调用该方法时,我的 WordSearchTest 类中出现错误。
标签: java multidimensional-array random