加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java-如何解析带有Cover数组的2d数组以查找字符?

发布时间:2020-12-14 19:25:39 所属栏目:Java 来源:网络整理
导读:基本上,我的任务是将消息存储在二维数组中,并且该消息附带一个封面消息,该消息是一系列的破折号和O,需要在特定坐标(行,列)上将其“放置”在原始消息上)以显示一条消息.我目前停留在如何将封面消息“放置”在原始消息上以解码文本的问题上.我的朋友告诉我解析

基本上,我的任务是将消息存储在二维数组中,并且该消息附带一个封面消息,该消息是一系列的破折号和O,需要在特定坐标(行,列)上将其“放置”在原始消息上)以显示一条消息.我目前停留在如何将封面消息“放置”在原始消息上以解码文本的问题上.我的朋友告诉我解析消息的封面,并编写一系列if语句,说“如果有o,则在二维数组中采用该维并将其添加到message变量中”.

这是消息:

“We hold these truths to be self-evident,that all men are created equal,that they are endowed by their Creator with certain unalienable Rights,that among these are Life,Liberty and the pursuit of Happiness. That to secure these rights,Governments are instituted among Men,deriving their just powers from the consent of the governed,–That whenever any Form of Government becomes destructive of these ends,it is the Right of the People to alter or to abolish it,and to institute new Government,laying its foundation on such principles and organizing its powers in such form,as to them shall seem most likely to affect their Safety and Happiness.”

这是封面消息:

-O------O-----O-------------------------
--O----O--------------------O------O----
------O---O-----------------------------
----------------------O--------------O--
------------------------------O-----O---
-----------------------------------O----
-------O---------------------O----------

任何帮助表示赞赏,谢谢.

编辑:到目前为止,我已经用消息填充了一个2d数组,并用封面消息填充了一个2d数组.封面消息应该适合于从[2] [5]开始的原始消息数组.希望这可以帮助.

import java.io.*;
import java.util.*;

public class M {

    public static void main(String[] args) throws FileNotFoundException {
        File inFile = new File("input.txt");
        Scanner scanFile = new Scanner(inFile);

        int lines;
        lines = scanFile.nextInt();

        String message = "";
        for (int i = 0; i <= lines; i++)
            message += scanFile.nextLine();

        message = message.replace(" ","");
        message = message.replace(",","");
        message = message.replace("-","");

        String[][] am = new String[lines][59];

        fill2DArray(am,message);
        print2DArray(am);

        System.out.println(Arrays.deepToString(am).replace("],"]n"));

        String r = scanFile.nextLine();
        r = r.replace(","");
        String ro = r.substring(0,1);
        String co = r.substring(1);

        int crow = Integer.parseInt(ro);
        int ccol = Integer.parseInt(co);

        int cline = scanFile.nextInt();
        System.out.println(cline);

        String cover = "";
        for (int u = 0; u <= cline; u++)
            cover += scanFile.nextLine();

        String[][] cm = new String[cline][40];

        fill(cm,cover);
        print2DArray(cm);
    }

    public static void fill2DArray(String[][] arr2D,String message)
    {
        int counterLetters = 0;

        for(int i =0;i<arr2D.length;i++) //arr.2D.length gives row length
        {
            for(int j = 0;j<arr2D[i].length;j++)//arr2D[].length gives column length
            {
                arr2D[i][j] = message.substring(counterLetters,counterLetters+1);
                counterLetters++;
            }
            System.out.println();
        }
    }

    public static void fill(String[][] arr2D,String cover)
    {
        int counterLetters = 0;

        for(int i =0;i<arr2D.length;i++) //arr.2D.length gives row length
        {
            for(int j = 0;j<arr2D[i].length;j++)//arr2D[].length gives column length
            {
                arr2D[i][j] = cover.substring(counterLetters,counterLetters+1);
                counterLetters++;
            }
            System.out.println();
        }
    }

    public static void print2DArray(String[][] arr2D)
    {
        for(int i =0;i<arr2D.length;i++) //arr.2D.length gives row length
        {
            for(int j = 0;j<arr2D[i].length;j++)//arr2D[].length gives column length
            {
                System.out.print(arr2D[i][j]);
            }
            System.out.println();
        }
    }
}
最佳答案
您可以做的是遍历数组的长度,并检查Cover数组中特定索引处的元素是否为O.如果是,则将消息数组中的元素添加到包含完整消息的字符串中:

// Assuming that the cover and message arrays are of type chars

String messageStr = "";
// Since the message and cover arrays should be the same length,it doesn't matter which one is used to get the length of the loop
for (int i = 0; i < messageArray.length; i++) {
    for (int j = 0; j < messageArray[i].length; j++) {
        if (cover[i][j] == 'O') {
            messageStr += messageArray[i][j];
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读