Java纵横字谜2d数组 包分配2; 导入java.util.Random; 导入java.util.Scanner; 公共类test2{ 公共静态void main(字符串[]args){ int-wordAmount=wordAmount(); String[]words=wordArray(wordAmount); 显示单词(单词、单词数量); char[][]puzzleGrid=generatePuzzleGrid(words,wordAmount); //System.out.println(Arrays.deepToString(拼图网格));//只需使用它来测试 打印拼图网格(拼图网格,字数); } 公共静态int-wordAmount(){ 扫描仪输入=新扫描仪(System.in); System.out.print(“您希望在单词搜索(5-15)中输入多少单词):”; int wordAmount=input.nextInt(); 而(字数20){ System.out.println(“您请求的数字要么太大,要么太小。”); System.out.print(“您希望在单词搜索中输入多少单词(5-20):”; wordAmount=input.nextInt(); } 返回字数; } 公共静态字符串[]字数组(int-wordAmount){ String[]words=新字符串[wordAmount]; 扫描仪输入=新扫描仪(System.in); for(int i=0;i
看来我上一个问题已经解决了,但现在我面临一个新问题 错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常:5 在assignment2.test2.generatePuzzleGrid处(test2.java:63) 位于assignment2.test2.main(test2.java:10) C:\Users\Higle\AppData\Local\NetBeans\Cache\8.2\executor snippets\run.xml:53:Java返回:1Java纵横字谜2d数组 包分配2; 导入java.util.Random; 导入java.util.Scanner; 公共类test2{ 公共静态void main(字符串[]args){ int-wordAmount=wordAmount(); String[]words=wordArray(wordAmount); 显示单词(单词、单词数量); char[][]puzzleGrid=generatePuzzleGrid(words,wordAmount); //System.out.println(Arrays.deepToString(拼图网格));//只需使用它来测试 打印拼图网格(拼图网格,字数); } 公共静态int-wordAmount(){ 扫描仪输入=新扫描仪(System.in); System.out.print(“您希望在单词搜索(5-15)中输入多少单词):”; int wordAmount=input.nextInt(); 而(字数20){ System.out.println(“您请求的数字要么太大,要么太小。”); System.out.print(“您希望在单词搜索中输入多少单词(5-20):”; wordAmount=input.nextInt(); } 返回字数; } 公共静态字符串[]字数组(int-wordAmount){ String[]words=新字符串[wordAmount]; 扫描仪输入=新扫描仪(System.in); for(int i=0;i,java,runtimeexception,Java,Runtimeexception,看来我上一个问题已经解决了,但现在我面临一个新问题 错误:线程“main”java.lang.ArrayIndexOutOfBoundsException中出现异常:5 在assignment2.test2.generatePuzzleGrid处(test2.java:63) 位于assignment2.test2.main(test2.java:10) C:\Users\Higle\AppData\Local\NetBeans\Cache\8.2\executor snippets\run.x
生成失败(总时间:9秒)看起来您正在使用少于5个字符的字符串调用charAt(5)。你可以这样做
package assignment2;
import java.util.Random;
import java.util.Scanner;
public class test2 {
public static void main(String[] args) {
int wordAmount = wordAmount();
String[] words = wordArray(wordAmount);
displayWords(words, wordAmount);
char[][] puzzleGrid = generatePuzzleGrid(words, wordAmount);
//System.out.println(Arrays.deepToString(puzzleGrid)); //just using this to test
printPuzzleGrid(puzzleGrid, wordAmount);
}
public static int wordAmount(){
Scanner input = new Scanner(System.in);
System.out.print("How many words would you like in the word search(5-15): ");
int wordAmount = input.nextInt();
while(wordAmount < 5 || wordAmount > 20){
System.out.println("The number you've requested is either to large or to small.");
System.out.print("How many words would you like in the word search(5-20): ");
wordAmount = input.nextInt();
}
return wordAmount;
}
public static String[] wordArray(int wordAmount){
String[] words = new String[wordAmount];
Scanner input = new Scanner(System.in);
for(int i = 0; i < wordAmount; i++){
System.out.print("Enter a word: ");
words[i] = (input.nextLine().toUpperCase());
for(int j = 0; j < wordAmount; j++){
if(j == i) break;
while(words[i].contains(words[j])){
System.out.print("The word you entered has already been entered, enter a new word: ");
words[i] = (input.nextLine().toUpperCase());
}
}
while(words[i].length() > 10 || words[i].length() <= 2 || words[i].contains(" ")){
System.out.print("The word you entered has been rejected, enter a new word: ");
words[i] = (input.nextLine().toUpperCase());
}
}
return words;
}
public static void displayWords(String[] words, int wordAmount){
System.out.print("The words you must find are: ");
for(int w = 0; w < wordAmount; w++){
System.out.print(words[w] + " ");
}
System.out.println("");
}
public static char[][] generatePuzzleGrid(String[] words, int wordAmount){
char[][] puzzleGrid = new char[wordAmount][wordAmount];
Random rand = new Random();
for(int across = 0; across < wordAmount; across++){
for(int down = 0; down < words[across].length(); down++){
puzzleGrid[across][down] = words[across].charAt(down);
for(int filler = wordAmount; filler >= words[across].length(); filler--){
puzzleGrid[across][filler] = (char)(rand.nextInt(26) + 'A'); //this is the line with the problem
}
}
}
return puzzleGrid;
}
public static void printPuzzleGrid(char[][] puzzleGrid, int wordAmount){
for(int across = 0; across < wordAmount; across++){
for(int down = 0; down < wordAmount; down++){
System.out.print(" " + puzzleGrid[down][across]);
}
System.out.println("");
}
}
}
if(单词[cross].length()<(down-1)){
继续;
}
为了确保你不会犯那个特别的错误。。。另外,您可能想知道charAt()从0->length()-1返回字符的索引,应用程序运行时不会出现任何问题。唯一的问题是,如果用户提供的单词数量(网格中的字符数)大于他提供的单词数量,会发生什么情况。因此,最好使用单词长度对wordAmount进行验证,如下所示
if(words[across].length() < (down-1)){
continue;
}
publicstaticchar[]generatePuzzleGrid(字符串[]个字,int-wordAmount){
如果(单词数量>单词长度){
wordAmount=words.length;
}
char[][]拼图网格=新字符[wordAmount][wordAmount];
for(int-cross=0;cross
您能解释一下wordAmount
和words
到底是什么,以及您在内部循环中试图实现什么吗?当您询问异常时,请始终发布准确完整的堆栈跟踪。它确切地告诉我们异常是什么,以及它是从哪个代码抛出的。您是否阅读了ArrayIndexOutOfBoundsException的文档?您是否使用调试器来找出它的来源?您正在尝试访问[跨]的单词。单词是否包含足够的元素?wordAmount是从用户处获得的整数,words是从用户处获得的字符串数组。什么可以防止单词从网格的右边缘跑掉?当您获得此异常(word和wordAmount值)时,您传递了哪些参数?否。这将抛出StringIndexOutOfBoundsException。
public static char[][] generatePuzzleGrid(String[] words, int wordAmount) {
if(wordAmount>words.length){
wordAmount = words.length;
}
char[][] puzzleGrid = new char[wordAmount][wordAmount];
for (int across = 0; across < wordAmount; across++) {
for (int down = 0; down < words[across].length(); down++) {
puzzleGrid[across][down] = words[across].charAt(down);/////////////////
}
}
return puzzleGrid;
}