当前位置: 代码迷 >> J2SE >> Java 求基础坚实人士赐教
  详细解决方案

Java 求基础坚实人士赐教

热度:95   发布时间:2016-04-23 20:05:18.0
Java 求基础扎实人士赐教
you are asked to write an encryption program. The program takes 3 inputs:

A letter of the alphabet that will remain unencoded (e.g. "J")
A 5-letter keyword (e.g. "BREAK")
A message to be encrypted (e.g. "COMPUTERSCIENCE"). You may assume that it does not contain any spaces or punctuation.
To form the code, first create a 5x5 matrix in which the first row is your keyword and the other elements are the remaining letters of the alphabet (minus the unencoded letter):
B R E A K
C D F G H
I L M N O
P Q S T U
V W X Y Z

Then, break the message into letter pairs and, for each letter, output the letter in the same row but at the position of the other one in the pair. So we have:
CO-MP-UT-ER-SC-IE-NC-E
The letter 'C' is in the second row, and the letter 'O' is in the 5th position, so we output the letter at row 2, position 5 ("H"). Next, the letter 'O' is in row 3, and the other letter of the pair ('C') is at position 1, so we output the letter at row 3, position 1 (i.e. 'I'); and so on. 
If there is an odd number of letters, as in this example, leave the final letter unaltered. So the final output is:
HIISTUREPFMBIGE
------解决思路----------------------

/**自己研究了半天,又问了下老师,勉强能写出来这么多,还需要做的事情是划分字母对,
*查找字母对中另一个字母的位置。我的矩阵是char[][]类型的c。已经累半死了,明天继续研究,
*如果有大神根据我的代码完成剩下的,我会很高兴。如果有人基于我的翻译解出这题,也算我有一点作用。
*/
import java.util.*;
public class Encryption {
public static void main(String []args)throws Exception{
//获得保留字母
System.out.print("请输入保留字母:");
Scanner sc1 = new Scanner(System.in);
String s = sc1.next();

char remain = s.toCharArray()[0];

if(s.toCharArray().length == 1){
System.out.println(remain);
}
else{
System.out.println("Error");
}

//获得Keyword

System.out.print("请输入5位Keyword : ");
Scanner sc2 = new Scanner(System.in);
char[] keyword = new char[5]; 
char[] cd = sc2.next().toCharArray();
for (int i = 0; i < keyword.length; i++) {
keyword[i] = cd[i];
}

// 获得原始信息

System.out.print("请输入原始信息:");
Scanner sc3 =new Scanner(System.in);
ArrayList en = new ArrayList();
char[] enr = sc3.next().toCharArray();
for (int j = 0; j < enr.length; j++) {
en.add(enr[j]);
}
Iterator i = en.iterator();
int longth = 0;//判断原始信息是不是奇数
while(i.hasNext()){
i.next();
longth +=1;}

char[] ch = new char[longth];
i = en.iterator();
while(i.hasNext()){
for (int j = 0; j < ch.length; j++) {
ch[j] = (char) i.next();
}
}System.out.println(Arrays.toString(ch));


// 建立矩阵
String word = new String("abcdefghijklmnopqrstuvwxyz");
char[] str = word.toCharArray();
char[][] c =new char[5][5]; 
int index = -1;


for (int j = 1; j < c.length; j++) {
int j2 = 0;
while(j2<c[j].length){
index ++;
boolean f1 = false,f2 = false;
if (index == word.indexOf(remain)) {
f1 = true;
}

for (int j4 = 0; j4 < keyword.length; j4++) {
boolean f = index == (new String(str)).indexOf(Character.toString(keyword[j4]));
if(f){
f2 = true;
}
}


// String keyword_str = new String(keyword);
//
// if(str[index] != remain && keyword_str.indexOf(str[index])<0){
// if(index>0&&index<str.length)
// c[j][j2] = str[index] ;
// j2++;
// }

if(!f1 && !f2){
if(index>0&&index<str.length)
c[j][j2] = str[index] ;
j2++;
}



}
}

//for (int j = 1; j < c.length; j++) {
for (int j2 = 0; j2 < c[0].length; j2++) {
c[0][j2] = keyword[j2];
}
// }

for (char[] ds : c) {
System.out.println(Arrays.toString(ds));

}

  相关解决方案