简体   繁体   English

如何使用随机对象将随机生成的字母打印到2D数组中

[英]How to print randomly generated letters using the random object into a 2D array

I'm trying to fill a 2D array with the reference variable called letters with randomly generated upper case letters using the Random object. 我正在尝试使用称为Random的随机变量生成的大写字母,用称为字母的参考变量填充2D数组。 I've tried doing it in both classes now but I still get a few errors that I've never encountered before. 我现在尝试在两个类中都这样做,但是仍然遇到一些以前从未遇到过的错误。 Any help on this is greatly appreciated. 在此方面的任何帮助将不胜感激。

Below are the errors I'm getting in the WordSearch class and where they are located: 以下是我在WordSearch类中遇到的错误以及它们的位置:

  • I get an error that says, "char someChar = (char)(rand.nextInt(26) + 65);" 我收到一个错误,说“ char someChar =(char)(rand.nextInt(26)+ 65);” The error reads, "Syntax error on token ";", { expected after this token." 该错误显示为“令牌“;”上的语法错误,{预期在此令牌之后”。

  • I'm also getting an error at the end of my for loop where the } is. 在}所在的for循环的结尾,我也遇到了错误。 The error reads, "Syntax error, insert "}" to complete Block." 该错误显示为“语法错误,插入“}”以完成“阻止”。

  • Lastly, I'm getting an error on the line that says, "public search(){" The error reads, "Return type for the method is missing." 最后,我在显示“ 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;
}

}

I am looking to see if I can find the cause of your first errors, but initially, the error about return type is easy; 我正在寻找是否可以找到您的第一个错误的原因,但是最初,关于返回类型的错误很容易。 every method in Java must specify a return type. Java中的每个方法都必须指定一个返回类型。 If the method returns nothing, and say--in this case--simply prints something to the console, the return type would be void . 如果该方法不返回任何内容,并且说(在这种情况下)仅向控制台输出内容,则返回类型为void

Change the method declaration to public void search() {} and it will eliminate that error. 将方法声明更改为public void search() {} ,它将消除该错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM