有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

我必须用字符填充2D数组,让人们搜索单词(Java)

所以我必须用字符填充一个2D数组,打印出数组,让人们搜索单词,然后打印出该单词的实例数,并用该单词的实例高亮度显示该数组

以下是我目前的代码:

import java.util.Scanner;
import java.util.*;

public class testSearchMatrix {

public static void printArray(char[][] myArray){
    for(int i = 0; i < myArray.length; i++){
        for(int j = 0; j < myArray.length; j++){
            System.out.print(myArray[i][j] + " ");
        }
        System.out.println();
    }
}

public static void searchArray(char[][] a){
    Scanner keyboard = new Scanner(System.in);
    System.out.println("Enter a query to search: ");
    String query = keyboard.next();
    int queryNum = 0;
    int w = 0;
    for(int i = 0; i < a.length; i++){
        for(int j = 0; j < a[i].length; j++){
            if(a[i][j] == query.charAt(w)){
                queryNum += 1;
            }
        }
    }
    System.out.println(queryNum);
}


public static void main(String[] args){

    Scanner keyboard = new Scanner(System.in);
    Random random = new Random();

    //Create an alphabet array so I can use this to fill in the searchBox array
    char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};

    System.out.println("Please choose an array size: ");
    int a = keyboard.nextInt();

    //Create a square array
    char[][] searchBox = new char[a][a];

    //Fill in the array with random chars
    for(int i = 0; i < searchBox.length; i++){
        for(int j = 0; j < searchBox[i].length; j++){
            int randNum = random.nextInt(25);
            searchBox[i][j] = alphabet[randNum];
        }
    }

    //Implement my method to print the array to the screen
    System.out.println("Here is the square matrix with random letters: ");
    printArray(searchBox);


    System.out.println("Enter a query to search: ");
    searchArray(searchBox);




}
}

这将打印出我的数组,但我似乎无法让搜索工作


共 (1) 个答案

  1. # 1 楼答案

    修改了searchArray函数

    public static void searchArray(char[][] a){
            Scanner keyboard = new Scanner(System.in);
            System.out.println("Enter a query to search: ");
            String query = keyboard.next();
            int queryNum = 0;
            String out = null;
            int w = 0;
            for(int i = 0; i < a.length; i++){
                for(int j = 0; j < a[i].length; j++){
                    if(a[i][j] == query.charAt(w)){
                        //System.out.println(i+":"+j+a[i][j]);
                        //w+=1;
                        if(out==null)
                        {
                            out=String.valueOf(a[i][j]);
                        }else
                        out=out+a[i][j];
                        
                        for(int f = 1; f < query.length(); f++){
                            if(j+f<5){
                        if(a[i][j+f] == query.charAt(w+f)){
                       //   System.out.println(i+"Index:w+f"+w+f+query.charAt(w+f)+"query.charAt(w+f)Index"+query.indexOf(query.charAt(w+f)));
                        //  System.out.println(i+":"+j+a[i][j+f]);
                            out=out+a[i][j+f];
                            System.out.println(out+":"+query+"here"+out.length()+query.length());
                            if(out.equals(query))
                            {
                                System.out.println("Seach Found ");
                                queryNum += 1;
                                out=null;
                            }
                        } 
                       
                            }
                            
                       
                        }  if(out!=null)
                           if(out.equals(query))
                    {
                        System.out.println("Seach Found ");
                        queryNum += 1;
                        out=null;
                    }  
                         out=null;
                    }
                }
            }
            System.out.println(queryNum);
        }
    

    输出

    OuptPut