gpt4 book ai didi

java - 如何使用随机对象将随机生成的字母打印到二维数组中

转载 作者:行者123 更新时间:2023-12-02 12:10:41 25 4
gpt4 key购买 nike

我正在尝试使用 Random 对象使用随机生成的大写字母来填充称为字母的引用变量的二维数组。我现在已经尝试在两个类(class)中执行此操作,但仍然遇到一些以前从未遇到过的错误。非常感谢对此的任何帮助。

以下是我在 WordSearch 类中遇到的错误及其位置:

  • 我收到一条错误消息“char someChar = (char)(rand.nextInt(26) + 65);”该错误显示为“标记“;”上的语法错误,{预期在此标记之后。”

  • 我还在 for 循环末尾处遇到错误,其中 } 所在的位置。错误内容为“语法错误,插入“}”以完成 block 。”

  • 最后,我在“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;
}

}

最佳答案

我想看看是否能找到您的第一个错误的原因,但最初,有关返回类型的错误很容易; Java 中的每个方法都必须指定返回类型。如果该方法不返回任何内容,并且说(在本例中)只是将某些内容打印到控制台,则返回类型将为 void

将方法声明更改为 public void search() {} 即可消除该错误。

关于java - 如何使用随机对象将随机生成的字母打印到二维数组中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46574626/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com