我正在制作一个程序,它接受一个短语,然后通过2个矩阵运行它来加密它,我遇到第二个矩阵的问题,在那里我翻译了短语eg.ADFGESZE并将其拆分并使用填充数组的字符,但是当我打印它时矩阵总是空白的问题我不知道为什么因为我查看了我的代码时间并且无法找到问题而现在我很困惑从Stack Overflow中提取代码块为了解决这个问题,如果你们中的任何一个人能够发现问题,我会把它发布在pastebin中。
我在哪里获得翻译:
package polycipher;
import java.util.Scanner;
public class Matrix {
private final char[] CIPHER_KEY = { 'A', 'D', 'F', 'G', 'V', 'X' }; // static morse array
private final String validChars = "ABCDEFGHIJ
KLMNOPQRSTUVWXYZ0123456789"; // characters which can be used
public String translation = ""; // for storing it the translation of the phrase
{ // failsafe, our program will be stuck in an infinite loop if
// number of valid characters is not at lease the square of CIPHER_KEY
if(validChars.length() < CIPHER_KEY.length*CIPHER_KEY.length) {
System.out.println("Error: Not enough valid characters to populate matrix.");
}
}
private char[][] matrix = new char[CIPHER_KEY.length][CIPHER_KEY.length]; // field for holding the generated matrix
private int[] usedNumbers = new int[validChars.length()]; // field for enforcing unique characters
{ // initiate all array elements to -1 (our "empty" value)
for (int x = 0; x < usedNumbers.length; x++)
usedNumbers[x] = -1;
}
public Matrix() { // Matrix default constructor override
int random;
for (int i = 0; i < CIPHER_KEY.length; i++) { // for every row
for (int j = 0; j < CIPHER_KEY.length; j++) { // for every column
validation: while (true) { // do this until I break the loop manually
random = (int) (Math.random() * validChars.length()); // generates a random number from 0 (inclusive) to (36 exclusive)
for (int k = 0; k < usedNumbers.length; k++) { // check every previously used character
if (random == usedNumbers[k]) {
continue validation; // if this char used before, skip it
} else if (usedNumbers[k] == -1) {
usedNumbers[k] = random; // if not previously used, store its value as used
break validation; // and break out of this validation loop
}
}
}
matrix[i][j] = validChars.split("")[random].charAt(0); // add this character to the matrix
}
}
}
public void searchMatrix(){
Scanner console = new Scanner (System.in);
String phrase ="";
System.out.println("\n Enter the message you would like "
+ "to encrypt with the cipher: ");
phrase = console.nextLine();
char[] phraseSplit = phrase.toUpperCase().toCharArray();
for (int k=0; k<phraseSplit.length; k++) {
for (int i=0; i<matrix.length; i++) {
for (int j=0; j<matrix[i].length; j++) {
if (phraseSplit[k] == matrix[i][j]) {
System.out.printf("%c has a Array Index %d, %d\n", phraseSplit[k], i, j);
if(i == 0){ translation += "A"; }
if(i == 1){ translation += "D"; }
if(i == 2){ translation += "F"; }
if(i == 3){ translation += "G"; }
if(i == 4){ translation += "V"; }
if(i == 5){ translation += "X"; }
if(j == 0){ translation += "A"; }
if(j == 1){ translation += "D"; }
if(j == 2){ translation += "F"; }
if(j == 3){ translation += "G"; }
if(j == 4){ translation += "V"; }
if(j == 5){ translation += "X"; }
}//end if
}//end for j
}//end for i
}//end for k
System.out.println("");
} //search end
public String toString() { // toString override, useful for debugging, prints out the matrix in a 6x6 table
String output = " A D F G V X\n";
for (int i = 0; i < CIPHER_KEY.length; i++) {
output += CIPHER_KEY[i] + " ";
for (int j = 0; j < CIPHER_KEY.length; j++) {
output += matrix[i][j] + " ";
}
output += "\n";
}
return output;
}
}
我填充并打印矩阵的地方:
package polycipher;
import java.util.*;
import polycipher.Matrix;
public class CipherKey {
Scanner write = new Scanner (System.in);
Matrix polyCipher = new Matrix();
private final char[] PHRASE_KEY = { 'J', 'A', 'V', 'A'};
String[][] matrixStore = new String[PHRASE_KEY.length][PHRASE_KEY.length];
public void assembleMatrix() {
polyCipher.translation = matrixFormatter(polyCipher.translation);
matrixStore = buildMatrix(polyCipher.translation,"|",",");
System.out.println(printMatrix(matrixStore));
}
public String printMatrix(String s [][]){
String keyOut = " J A V A\n";
for (int i = 0; i < s.length; i++) {
// keyOut += PHRASE_KEY[i] + " ";
for (int j = 0; j < s[i].length; j++) {
keyOut += s[i][j] + " ";
}
keyOut += "\n";
}
return keyOut;
}
public static String [][] buildMatrix (String translation, String outermarker, String innermarker) {
// outerdelim may be a group of characters
String [] sOuter = translation.split ("[" + outermarker + "]");
int size = sOuter.length;
// one dimension of the array has to be known on declaration:
String [][] result = new String [size][];
int count = 0;
for (String line : sOuter)
{
result [count] = line.split (innermarker);
++count;
}
return result;
}
public String matrixFormatter(String x){
String resultstr = "";
int i = 0;
while(i < x.length()) {
// If end of string: only add character.
if (i == x.length() - 1) {
resultstr += x.substring(i, i + 1);
} else {
if ( ((i + 1) % 4) == 0) {
resultstr += x.substring(i, i + 1) + "|";
} else {
resultstr += x.substring(i, i + 1) + ",";
}
}
i++;
}
return resultstr;
}
}
package polycipher;
/* This class will be for inplementing methods in the main and creating a GUI basically */
import polycipher.Matrix;
import polycipher.FileParsing;
import polycipher.CipherKey;
public class ApplicationDriver {
public static void main(String[] args) {
//Create and print the matrix
Matrix polyCipher = new Matrix();
System.out.print(polyCipher);
//Search the matrix with our message
polyCipher.searchMatrix();
System.out.println("Sting translated: " + polyCipher.translation);
System.out.println("Corordinates on Cipher: " +
java.util.Arrays.toString(polyCipher.translation.split("(?<=\\G..)")));
CipherKey Key = new CipherKey();
Key.assembleMatrix();
//Select our file
FileParsing secretFile = new FileParsing();
secretFile.openFile();
//Add the phrase to the file
secretFile.addPhrase(polyCipher.translation);
//Close the file
secretFile.closeFile();
}
}
输出:
A D F G V X
A B Q J 8 E R
D Z 0 L 6 5 9
F D 3 Y T I X
G C 4 O V W A
V F M N P K S
X U G H 1 7 2
Enter the message you would like to encrypt with the cipher:
gjhjkjvgjv
G has a Array Index 5, 1
J has a Array Index 0, 2
H has a Array Index 5, 2
J has a Array Index 0, 2
K has a Array Index 4, 4
J has a Array Index 0, 2
V has a Array Index 3, 3
G has a Array Index 5, 1
J has a Array Index 0, 2
V has a Array Index 3, 3
Sting translated: XDAFXFAFVVAFGGXDAFGG
Corordinates on Cipher: [XD, AF, XF, AF, VV, AF, GG, XD, AF, GG]
J A V A
Enter the .txt file you want to store your phrase in:
矩阵应出现在J A V A下面,如下所示:
J A V A
x x x x
x x x x
x x x x
例如