簡體   English   中英

如何將一個2D數組覆蓋在另一個數組上並提取字符?

[英]How to overlay one 2D array over another and extract characters?

我的任務是讀入一條消息和密鑰,然后在解碼之前覆蓋兩者。 message.txt文件在表NxN中包含隨機字符。 key.txt包含“ x”和“”,一旦放置在消息上將顯示一個秘密。 我的代碼存在問題,當我嘗試打印覆蓋版本時,我再次獲得了密鑰。 我的overlay方法的內循環是錯誤的還是其他? 我的意圖是用x覆蓋message.txt文件中的所有內容,以使剩下的/引人注意的是組成代碼的字母。

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

public class project1
{
   public static void main(String[] args)
   {
      int i = 0;
      int j = 0;
      int row = 0;
      String s = "";
      char[][] message = new char [20][20];
      char[][] key = new char [20][20];
      char[][] overlay = new char [20][20];

      message = message(row, i, j, s, message);
      key = key(row, i, j, s, key);
      overlay = overlay(row, i, j, key, message, overlay);


      print2DArray(row, i, j, s, message, key, overlay);

   }

   public static char[][] message(int row, int i, int j, String s, char[][] message)
   {
      File f;
      Scanner inFile = null;
      try {
         f = new File("secret_message.txt" );
         inFile = new Scanner( f );
      } catch ( IOException e ) {
         System.out.println( "Error opening : " + "secret_message.txt");
      }   

      while(inFile.hasNextLine()) //determine n X n
      {
         String nxn = inFile.nextLine();
         row++ ;
      }

      try {
         f = new File("secret_message.txt" );
         inFile = new Scanner( f );
      } catch ( IOException e ) {
         System.out.println( "Error opening : " + "secret_message.txt");
      }

      for(i = 0; i < row; i++ ) //row; storing message
      {
         s = inFile.nextLine();
         for(j = 0; j < row; j++) //column
         {
            message[i][j]= s.charAt(j);
         }
      }
      inFile.close();

      return message;
   }

   public static char[][] key(int row, int i, int j, String s, char[][] key)
   {
      File f;
      Scanner inFile = null;
      try {
         f = new File("key.txt" );
         inFile = new Scanner( f );
      } catch ( IOException e ) {
         System.out.println( "Error opening : " + "key.txt");
      }   

      while(inFile.hasNextLine()) //determine n X n
      {
         String nxn = inFile.nextLine();
         row++ ;
      }

      try {
         f = new File("key.txt" );
         inFile = new Scanner( f );
      } catch ( IOException e ) {
         System.out.println( "Error opening : " + "key.txt");
      }

      for(i = 0; i < row; i++ ) //row; storing key
      {
         s = inFile.nextLine();
         for(j = 0; j < row; j++) //column
         {
            key[i][j]= s.charAt(j);
         }
      }
      inFile.close();  
      return key;
   }

   public static char[][] overlay(int row, int i, int j, char[][] message, char[][] key, char[][] overlay)
   {
      for(i = 0; i < row; i++)
         for(j = 0; j < row; j++)
         {
            if(key[i][j] == 'x')
               message[i][j] = key[i][j];

         }
      return message;

   }   

   /*public static String decodeMessage( )
   {
      for (j = overlay[0].length-1; j >= 0; j--)
         for(i = overlay[0].length-1; i >= 0; i--)
         {
            =message[i][j]
         }

   }

   */

   public static void print2DArray(int row, int i, int j, String s, char[][] message, char[][] key, char[][] overlay)
   {
      for(i = 0; i< message.length; i++) //print out message
      {
         for(j = 0; j < message[i].length; j++)
            {
            System.out.print(message[i][j]);
            }
         System.out.println();
      }
      System.out.println();
      for(i = 0; i< key.length; i++) //print out key
      {
         for(j = 0; j < key[i].length; j++)
            {
            System.out.print(key[i][j]);
            }
         System.out.println();
      }
      System.out.println();
      for(i = 0; i< overlay.length; i++) //print out overlay
      {
         for(j = 0; j < overlay[i].length; j++)
            {
            System.out.print(overlay[i][j]);
            }
         System.out.println();
      }
   }
}

首先,看看您在overlay()參數序列:

public static char[][] overlay(int row, int i, int j,
    char[][] message, char[][] key, char[][] overlay)

現在是在main()調用它的方式:

overlay = overlay(row, i, j, key, message, overlay);

看,當方法需要message, key, overlay時,您正在傳遞key, message, overlay message, key, overlay

然后,在您的overlay()方法中,您似乎希望將循環內的邏輯更改為此:

if (key[i][j] == 'x')
    overlay[i][j] = message[i][j];
else
    overlay[i][j] = ' ';

以及返回overlay而不是message

return overlay;

替代實施

出於教育目的,這是使用ArrayList<String>代替char[][]類的簡化版本:

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

public class Scratch
{
    public static void main(String[] args) throws IOException {
        List<String> message = read("secret_message.txt");
        print(message);
        List<String> key = read("key.txt");
        print(key);
        List<String> overlay = overlay(message, key);
        print(overlay);
    }

    public static List<String> read(String filePath) throws IOException {
        List<String> lines = new ArrayList<String>();
        File f = new File(filePath);
        try (Scanner inFile = new Scanner(f)) {
            while (inFile.hasNextLine()) {
                lines.add(inFile.nextLine());
            }
        }
        return lines;
    }

    public static List<String> overlay(List<String> msg, List<String> key) {
        List<String> overlay = new ArrayList<String>();
        for (int i = 0; i < msg.size() && i < key.size(); i++) {
            StringBuilder line = new StringBuilder();
            for (int j = 0; j < msg.get(i).length() && j < key.get(i).length(); j++) {
                line.append(key.get(i).charAt(j) == 'x' ? msg.get(i).charAt(j)
                        : ' ');
            }
            overlay.add(line.toString());
        }
        return overlay;
    }

    public static void print(List<String> lines) {
        for (String line : lines) {
            System.out.println(line);
        }
        System.out.println();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM