[英]A simple cypher program in java
I'm trying to make a simple cipher in java that replaces value in an array and replaces it with a value in another array. 我正在尝试在java中创建一个简单的密码,该密码将替换数组中的值,并将其替换为另一个数组中的值。 One being a number, and the next being an alphabet.
一个是数字,第二个是字母。
I'm very new to Java, like last week new, and I'm still trying to understand the basics.The only way I have figured out to do this would be to declare each value and its equivalent, is there a shorter way to do this without adding 25 more unnecessary lines of code? 我对Java非常陌生,就像上周刚接触Java,但我仍在尝试了解基础知识。我想做到这一点的唯一方法是声明每个值及其等效项,还有一种更短的方法这样做无需添加25条多余的代码行? I'm not too sure where to proceed from here.
我不太确定从哪里开始。 Any help would be greatly appreciated.
任何帮助将不胜感激。
class simpleCypher {
String[] alpha;
String[] numo;
public static void main(String args[]) {
String[] numo = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26",};
String[] alpha = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z", };
numo[21] = alpha[21];
System.out.println(numo[21]);
}
}
If you want to associate each number with a specific character ; 如果您想将每个数字与一个特定字符相关联; for example every time the program sees "1" it replaces it with "a" , I would say you should go for a HashMap.
例如,每次程序看到“ 1”时,它就会用“ a”替换它,我想说您应该使用HashMap。 A HashMap is a data structure that stores two things a Key & a Value.
HashMap是一种存储键和值两件事的数据结构。 each value is associated with a key and the HashMap maps unique keys to values.
每个值都与一个键相关联,并且HashMap将唯一键映射到值。
The code would be as following: 代码如下:
class simpleCypher {
private static HashMap<String, String> hMap = new HashMap<>();
public static void main(String[] args){
hMap.put("1", "a");
hMap.put("2", "b");
hMap.put("3", "c");
// continue adding ...
String[] numo = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11",
"12", "13", "14", "15", "16", "17", "18",
"19", "20", "21", "22", "23", "24", "25", "26",};
for (int i = 0; i<numo.length ; i++){
numo[i] = hMap.get(numo[i]);
System.out.println(numo[i]);
}
}
}
I advise you to use a Map and apply your cipher logic for each entry. 我建议您使用映射,并对每个条目应用密码逻辑。 Something like that:
像这样:
public static void main(String args []) {
Map<Integer, Integer > map = new HashMap<>();
for ( int i = 1; i <= 25; i ++ ){
// do whatever you want in the logic
int cypher = i * i + 1;
map.put(i, cypher);
}
map.values().stream().forEach(cypher -> System.out.println(cypher));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.