基本上,我的任务是将消息存储在二维数组中,并且该消息附带一个封面消息,该消息是一系列破折号和O,需要在特定坐标处将其“放置”在原始消息上(行,列)以显示一条消息。我目前停留在如何将封面消息“放置”在原始消息上以解码文本的问题上。我的朋友告诉我解析消息的封面,并写出一系列if
语句,说“如果有o,则在二维数组中采用该维并将其添加到消息变量中”。
这是消息:
“我们认为这些真理是不言而喻的,所有人都是平等的,创造者赋予他们某些不可剥夺的权利,其中包括生命,自由和对幸福的追求。确保这些权利,政府是在人中间建立的,其正当权力来自被统治者的同意,即,只要任何形式的政府破坏了这些目的,人民就有权改变或废除并确立新政府将以这些原则为基础,以这种形式组织其权力,因为它们似乎最有可能影响其安全和幸福。”
这是封面消息:
-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();
}
}
}
答案 0 :(得分:2)
您可以做的是遍历数组的长度,并检查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];
}
}
}