gpt4 book ai didi

java - 如何使用覆盖数组解析我的二维数组以查找字符?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:06:21 24 4
gpt4 key购买 nike

基本上,我的任务是将一条消息存储在一个二维数组中,该消息带有一个封面消息,它是一系列破折号和 O,需要在特定坐标处“放置”在原始消息上(行、列)以显示消息。我目前停留在如何将封面消息“放置”在原始消息上以解码文本。我的 friend 告诉我解析消息的封面并编写一系列 if 语句,表示“如果有 o,则在二维数组中获取该维度并将其添加到消息中多变的”。

这是消息:

"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----------

感谢任何帮助,谢谢。

编辑:到目前为止,我已经用消息填充了一个二维数组,用封面消息填充了一个二维数组。封面消息应该覆盖从 [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();
}
}
}

最佳答案

您可以做的是遍历数组的长度并检查覆盖数组中特定索引处的元素是否为 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];
}
}
}

关于java - 如何使用覆盖数组解析我的二维数组以查找字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54395089/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com