向数组添加边框 [英] Adding a border to an array

查看:191
本文介绍了向数组添加边框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  WVERTICALL 

ROOAFFLSAB

ACRILIATOA

NDODKONWDC

DRKESOODDK

OEEPZEGLIW

MSIIHOAERA

ALRKRRIRER

KODIDEDRCD

HELWSLEUTH

我怎样才能得到它打印这样的:

 ############

#WEDTICALL#

#ROOAFFLSAB#

#ACRILIATOA#

#NDODKONWDC#

#DRKESOODDK #

#OEEPZEGLIW#

#MSIIHOAERA#

#ALRKRRIRER#

#KODIDEDRCD#

#HELWSLEUTH#

###########

我已经试过让尺寸更大来容纳额外的字符,但我一直得到超出界限。我基本上试图得到一个非字母字符,使我的数组周围的边框,以便当我实现我的填字游戏求解器时,搜索周围的字母中的一个字母,它不会碰到边界,而是命中非字母字符然后传递false。



这是我的代码构建我的数组:

  String getChar =(new String(sc.next())); (int j = 1; j< col; j ++){
puzzle [i] [j] = getChar.charAt(j);


code $


$ p



编辑:这打印我的数组:

  for(int i = 0; i< puzzle .length; i ++){

System.out.print(Arrays.toString(puzzle [i]));
System.out.println();





编辑2:这是我的检查方法:

  private static boolean checkNE(int row,int col,String word,char [] [] puzzle){
//检查对角线方向$ b $如果(拼音字母= 1;字母< word.length();字母++){
if(拼字[行 - 字母] [col +字母]!= word.charAt(字母)){
返回false;
}
}
返回true;


解决方案

只要检查你要检查的位置是否在数组之外,如果是,那么你就知道你不会找到整个单词,因为我们将不得不去掉数组。例如,你的数组是 nxn ,如果你想访问位置 n 那么它就会出界,或者如果你试图访问一个小于0的位置,它也会出界。所以只要确保你在访问它的范围内。


I have an array that when printed, prints like this:

    W V E R T I C A L L 

    R O O A F F L S A B 

    A C R I L I A T O A 

    N D O D K O N W D C 

    D R K E S O O D D K 

    O E E P Z E G L I W 

    M S I I H O A E R A 

    A L R K R R I R E R 

    K O D I D E D R C D 

    H E L W S L E U T H

How can I get it to print like this:

    # # # # # # # # # # # #

    # W V E R T I C A L L #

    # R O O A F F L S A B #

    # A C R I L I A T O A #

    # N D O D K O N W D C #

    # D R K E S O O D D K #

    # O E E P Z E G L I W #

    # M S I I H O A E R A #

    # A L R K R R I R E R #

    # K O D I D E D R C D #

    # H E L W S L E U T H #

    # # # # # # # # # # # #

I have tried making the size larger to accommodate for the extra characters but I keep getting exception out of bounds. I am basically trying to get a non letter character to make a border around my array so that when I implement my crossword solver, when searching for a letter in the surrounding letters, it doesn't hit the boundary, instead hits the non letter character and then passes false.

This is my code that builds my array:

    for (int i=1; i<row; i++){
        String getChar = (new String(sc.next()));
        for(int j = 1;j<col; j++){
            puzzle[i][j] = getChar.charAt(j);
            }
        }

Any help is appreciated!

EDIT: This prints my array:

     for (int i=0; i<puzzle.length; i++){

           System.out.print(Arrays.toString(puzzle[i]));
           System.out.println("");
     }

EDIT2: This is my check method:

    private static boolean checkNE(int row, int col, String word, char[][] puzzle) {
    //Checking diagonals direction
    for(int letter = 1; letter < word.length(); letter++) {
        if(puzzle[row - letter][col + letter] != word.charAt(letter)) {
            return false;
        }
    }
    return true;
}

解决方案

Instead of making a border, just check to see if the position you're about to check is outside of the array, and if it, then you know you won't find the whole word as we would have to go outside of the array.

For example, you're array is n x n, if you're trying to access position n then it'll be out of bounds, or if you try to access a position less than 0 it will also be out of bounds. So just make sure when you're accessing it's within this range.

这篇关于向数组添加边框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆