2016-02-29 39 views
2

我是新來的Java和一般編碼,並且正在爲一個類進行密碼學項目。我創建了一個代碼,首先將字母轉換爲數字例如a=1,b=2等。下一部分通過分配數字來添加或減去編碼消息中的下一個字符,從而使數字混亂。 (參見圖片attatched)信號和混雜編號Java - 如何添加到字符串中的字符

Character  Letter Number   Jumble Number 
_______________|_______________________|_______________ 
A    1      +3 
B    2      -1 
C    3      +2 
D    4      +5 
E    5      -3 

Example Image

實施例因此,作爲一個例子,假設需要被髮送的消息是「ABCDE」。所以「a」的數字是1,但它也帶有一個「+3」,它將被添加到下一個字符的數字中,所以不是2,它將是5,依此類推。最後的信息將顯示爲「1,5,2,6,10」。

到目前爲止,我已經能夠將字母轉換爲數字,但不知道如何繼續對已經從字母轉換的數字加/減數字。這是迄今爲止我所擁有的。

import java.util.HashMap; 
    import java.util.Map; 


    public class CodeApp 
    { 
    public static void main(String[] args) 
    { 
final Map<Character, Integer> map; 
final String str = "hello world"; 
// Message to be encoded goes here 

map = new HashMap<>(); 

map.put('a', 1); 
map.put('b', 2); 
map.put('c', 3); 
map.put('d', 4); 
map.put('e', 5); 
map.put('f', 6); 
map.put('g', 7); 
map.put('h', 8); 
map.put('i', 9); 
map.put('j', 10); 
map.put('k', 11); 
map.put('l', 12); 
map.put('m', 13); 
map.put('n', 14); 
map.put('o', 15); 
map.put('p', 16); 
map.put('q', 17); 
map.put('r', 18); 
map.put('s', 19); 
map.put('t', 20); 
map.put('u', 21); 
map.put('v', 22); 
map.put('w', 23); 
map.put('x', 24); 
map.put('y', 25); 
map.put('z', 26); 
// This code is inefficient like this because I eventually plan to scramble the order of the numbers in relation to the letters, like a=23, b=5, c=15, etc. 
for(final char c : str.toCharArray()) 
{ 
    final Integer val; 

    val = map.get(c); 

    if(val == null) 
    { 
    // some sort of error 
    } 
    else 
    { 
    System.out.print(val + " "); 
    } 
} 

System.out.println(); 
    } 
} 

任何和所有的幫助,非常感謝!

+2

哪裏了'+ 3'從何而來? – Ian2thedv

+0

如果我理解正確,您提供的圖像中的「+3,-1,+2,+5,-3」可以同化到[key](https://en.wikipedia.org/wiki/Key_ %28cryptography%29)的密碼系統? – Spotted

+0

另外,如果'e'是純文本消息的最後一個字符,那麼它的相關數字(在本例中爲'-3')就沒用了,對吧? – Spotted

回答

1

你可以嘗試這樣做使用兩個的HashMap波紋管:

String str = "abcde"; 

Map<Character, Integer> charMap = new HashMap<Character, Integer>();// value of character 
charMap.put('a', 1); 
charMap.put('b', 2); 
charMap.put('c', 3); 
charMap.put('d', 4); 
charMap.put('e', 5); 
// put more entry for rest of the characters 

Map<Character, Integer> jumblMap = new HashMap<Character, Integer>();// value to add 
jumblMap.put('a', 3);  
jumblMap.put('b', -1); 
jumblMap.put('c', 2); 
jumblMap.put('d', 5); 
jumblMap.put('e', -3); 
// put more entry for rest of the characters 

char c = str.charAt(0); 
int val = charMap.get(c);// No jumble value for first character 
System.out.print(val + " "); 
char prevC; 
for(int i = 1; i < str.length(); i++) { 
    c = str.charAt(i); 
    prevC = str.charAt(i - 1);// previous character 
    val = charMap.get(c) + jumblMap.get(prevC);// evaluate with jumble value of previous char 
    System.out.print(val + " "); 
} 

輸出:

1 5 2 6 10 

這裏,最初將所有字符值和雜亂值存儲在兩個HashMap中。

charMap - 保存每個字符值

jumblMap - 保存混雜值

charMap.get(c) + jumblMap.get(PrevC) - 測定值假定所有字符在上述每個映射項。

+0

好的,謝謝,但是這會增加或減少賦予加/減的字母數字。所以它將是a = 1,然後將3加到1.我想要的是將3加到加密代碼中的下一個字符,在這種情況下是b = 2。如果數字被添加到/從它分配的字符中減去,它總是會像代碼中那樣結束,所以'a'可能等於4並且'b'可能等於1.幫助將會是感謝,謝謝! –

+0

@ D.Collins,請檢查我的更新答案。它應該滿足您的要求。 – mmuzahid

+0

非常非常感謝! –

0

作爲一個概念性解決方案,我建議將每個值添加到代碼列表中,然後將其轉換爲輸出。例如:

List<Integer> codes = new ArrayList<Integer>(); 

for(final char c : str.toCharArray()) 
{ 
    final Integer val; 

    val = map.get(c); 

    if(val == null) 
    { 
    // some sort of error 
    } 
    else 
    { 
    // don't try to output here 
    //System.out.print(val + " "); 

    // add this code to the list 
    codes.Add(val);   
    } 
} 
// transform codes list into string delimited text and output 
1

這裏你有一個錯誤的代碼,將你的變量定義爲for循環。 下面的代碼是我的建議,第一,你可以定義你的HashMap象下面這樣:

final Map<Character, String> map; 
map.put('a', "1"); 

,並做到在循環:

String val=""; 
    for(final char c : str.toCharArray()) { 
     if(map.get(c)!=null) { 
      val = val + map.get(c).toString(); 
    } else { 
    // some sort of error 
    } 
} 
0

不是一個答案:

一個棘手的方法來獲得字母序列是

String str = "AbCdEf"; 
for (char c : str.toLowerCase().toCharArray()) { 
    int val = Character.toLowerCase(c) - 'a' + 1; 
    System.out.print(val + " "); 
} 

輸出:1 2 3 4 5 6