java-如何解析带有Cover数组的2d数组以查找字符?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java-如何解析带有Cover数组的2d数组以查找字符?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3821字,纯文字阅读大概需要6分钟。
内容图文
基本上,我的任务是将消息存储在二维数组中,并且该消息附带一个封面消息,该消息是一系列的破折号和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];
}
}
}
内容总结
以上是互联网集市为您收集整理的java-如何解析带有Cover数组的2d数组以查找字符?全部内容,希望文章能够帮你解决java-如何解析带有Cover数组的2d数组以查找字符?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。