2014-07-12 68 views
0

我試圖編寫一個程序來解碼Adobe密鑰代碼到文本中,但是我使用的字符串數組似乎沒有更改值時傳遞通過我的for循環中的if語句。下面是代碼(我還沒有實現打印輸出部分,因爲我不能讓我的數組轉換值工作的測試):在for循環if語句似乎不影響String []值

import java.util.Scanner; 

public class Decode { 

    public static void main(String[] args) { 

     Scanner input = new Scanner(System.in); 
     System.out.println ("Enter collected data:"); 
     String data = input.nextLine(); 
     input.close(); 
     String[] splitdata = data.split("\\s+"); 
     int datalength=splitdata.length; 
     for (int i=0; i<datalength; i++){ 
      if (splitdata[i]=="1") 
       splitdata[i]="\n"; 
      if (splitdata[i]=="8") 
       splitdata[i]="**backspace**"; 
      if (splitdata[i]=="9") 
       splitdata[i]="**tab**"; 
      if (splitdata[i]=="13") 
       splitdata[i]="**enter**"; 
      if (splitdata[i]=="16") 
       splitdata[i]="**shift**"; 
      if (splitdata[i]=="17") 
       splitdata[i]="**control**"; 
      if (splitdata[i]=="19") 
       splitdata[i]="**pause/break**"; 
      if (splitdata[i]=="20") 
       splitdata[i]="**capslock**"; 
      if (splitdata[i]=="27") 
       splitdata[i]="**esc**"; 
      if (splitdata[i]=="32") 
       splitdata[i]="\\s+"; 
      if (splitdata[i]=="33") 
       splitdata[i]="*page up**"; 
      if (splitdata[i]=="34") 
       splitdata[i]="**page down**"; 
      if (splitdata[i]=="35") 
       splitdata[i]="**end**"; 
      if (splitdata[i]=="36") 
       splitdata[i]="**home**"; 
      if (splitdata[i]=="37") 
       splitdata[i]="**left**"; 
      if (splitdata[i]=="38") 
       splitdata[i]="**up**"; 
      if (splitdata[i]=="39") 
       splitdata[i]="**right**"; 
      if (splitdata[i]=="40") 
       splitdata[i]="**down**"; 
      if (splitdata[i]=="45") 
       splitdata[i]="**insert**"; 
      if (splitdata[i]=="46") 
       splitdata[i]="**delete**"; 
      if (splitdata[i]=="48") 
       splitdata[i]="0"; 
      if (splitdata[i]=="49") 
       splitdata[i]="1"; 
      if (splitdata[i]=="50") 
       splitdata[i]="2"; 
      if (splitdata[i]=="51") 
       splitdata[i]="3"; 
      if (splitdata[i]=="52") 
       splitdata[i]="4"; 
      if (splitdata[i]=="53") 
       splitdata[i]="5"; 
      if (splitdata[i]=="54") 
       splitdata[i]="6"; 
      if (splitdata[i]=="55") 
       splitdata[i]="7"; 
      if (splitdata[i]=="56") 
       splitdata[i]="8"; 
      if (splitdata[i]=="57") 
       splitdata[i]="9"; 
      if (splitdata[i]=="S-65") 
       splitdata[i]="a"; 
      if (splitdata[i]=="S-66") 
       splitdata[i]="b"; 
      if (splitdata[i]=="S-67") 
       splitdata[i]="c"; 
      if (splitdata[i]=="S-68") 
       splitdata[i]="d"; 
      if (splitdata[i]=="S-69") 
       splitdata[i]="e"; 
      if (splitdata[i]=="S-70") 
       splitdata[i]="f"; 
      if (splitdata[i]=="S-71") 
       splitdata[i]="g"; 
      if (splitdata[i]=="S-72") 
       splitdata[i]="h"; 
      if (splitdata[i]=="S-73") 
       splitdata[i]="i"; 
      if (splitdata[i]=="S-74") 
       splitdata[i]="j"; 
      if (splitdata[i]=="S-75") 
       splitdata[i]="k"; 
      if (splitdata[i]=="S-76") 
       splitdata[i]="l"; 
      if (splitdata[i]=="S-77") 
       splitdata[i]="m"; 
      if (splitdata[i]=="S-78") 
       splitdata[i]="n"; 
      if (splitdata[i]=="S-79") 
       splitdata[i]="o"; 
      if (splitdata[i]=="S-80") 
       splitdata[i]="p"; 
      if (splitdata[i]=="S-81") 
       splitdata[i]="q"; 
      if (splitdata[i]=="S-82") 
       splitdata[i]="r"; 
      if (splitdata[i]=="S-83") 
       splitdata[i]="s"; 
      if (splitdata[i]=="S-84") 
       splitdata[i]="t"; 
      if (splitdata[i]=="S-85") 
       splitdata[i]="u"; 
      if (splitdata[i]=="S-86") 
       splitdata[i]="v"; 
      if (splitdata[i]=="S-87") 
       splitdata[i]="w"; 
      if (splitdata[i]=="S-88") 
       splitdata[i]="x"; 
      if (splitdata[i]=="S-89") 
       splitdata[i]="y"; 
      if (splitdata[i]=="S-90") 
       splitdata[i]="z"; 
      if (splitdata[i]=="96") 
       splitdata[i]="0"; 
      if (splitdata[i]=="97") 
       splitdata[i]="1"; 
      if (splitdata[i]=="98") 
       splitdata[i]="2"; 
      if (splitdata[i]=="99") 
       splitdata[i]="3"; 
      if (splitdata[i]=="100") 
       splitdata[i]="4"; 
      if (splitdata[i]=="101") 
       splitdata[i]="5"; 
      if (splitdata[i]=="102") 
       splitdata[i]="6"; 
      if (splitdata[i]=="103") 
       splitdata[i]="7"; 
      if (splitdata[i]=="104") 
       splitdata[i]="8"; 
      if (splitdata[i]=="105") 
       splitdata[i]="9"; 
      if (splitdata[i]=="106") 
       splitdata[i]="*"; 
      if (splitdata[i]=="107") 
       splitdata[i]="+"; 
      if (splitdata[i]=="109") 
       splitdata[i]="-"; 
      if (splitdata[i]=="110") 
       splitdata[i]="."; 
      if (splitdata[i]=="111") 
       splitdata[i]="/"; 
      if (splitdata[i]=="112") 
       splitdata[i]="**F1**"; 
      if (splitdata[i]=="113") 
       splitdata[i]="**F2**"; 
      if (splitdata[i]=="114") 
       splitdata[i]="**F3**"; 
      if (splitdata[i]=="115") 
       splitdata[i]="**F4**"; 
      if (splitdata[i]=="116") 
       splitdata[i]="**F5**"; 
      if (splitdata[i]=="117") 
       splitdata[i]="**F6**"; 
      if (splitdata[i]=="118") 
       splitdata[i]="**F7**"; 
      if (splitdata[i]=="119") 
       splitdata[i]="**F8**"; 
      if (splitdata[i]=="120") 
       splitdata[i]="**F9**"; 
      if (splitdata[i]=="122") 
       splitdata[i]="**F11**"; 
      if (splitdata[i]=="123") 
       splitdata[i]="**F12**"; 
      if (splitdata[i]=="124") 
       splitdata[i]="**F13**"; 
      if (splitdata[i]=="125") 
       splitdata[i]="**F14**"; 
      if (splitdata[i]=="126") 
       splitdata[i]="**F15**"; 
      if (splitdata[i]=="144") 
       splitdata[i]="**NumLock**"; 
      if (splitdata[i]=="145") 
       splitdata[i]="**ScrLock**"; 
      if (splitdata[i]=="186") 
       splitdata[i]=";"; 
      if (splitdata[i]=="187") 
       splitdata[i]="="; 
      if (splitdata[i]=="188") 
       splitdata[i]=","; 
      if (splitdata[i]=="189") 
       splitdata[i]="-"; 
      if (splitdata[i]=="190") 
       splitdata[i]="."; 
      if (splitdata[i]=="191") 
       splitdata[i]="/"; 
      if (splitdata[i]=="192") 
       splitdata[i]="`"; 
      if (splitdata[i]=="219") 
       splitdata[i]="["; 
      if (splitdata[i]=="220") 
       splitdata[i]="**backslash**"; 
      if (splitdata[i]=="221") 
       splitdata[i]="]"; 
      if (splitdata[i]=="222") 
       splitdata[i]="'"; 

     } 

     System.out.println(splitdata[0]); 

    } 

} 

例如,如果我輸入8我應該得到的打印是讀取退格,但它只是輸出8.我敢肯定,這是一個簡單的錯誤,我忽略了,但我很新的Java所以任何幫助將不勝感激。

+1

不要使用字符串比較使用'=='來代替'.equals()'第二你可以告訴輸入的格式嗎? – Arvind

+0

輸入只是鍵入控制檯的字符串。那麼它會是:if(splitdata [i] .equals(「8))? – hwilco

+0

查看交換語句(Java 7+)或HashMaps來清理它(並使其更快)。 –

回答

0

試試這個例子:

import java.util.Scanner; 

class Ideone 
{ 
    public static void main (String[] args) throws java.lang.Exception 
    { 
     Scanner input = new Scanner(System.in); 
     System.out.println ("Enter collected data:"); 
     String data = input.nextLine(); 
     input.close(); 
     String[] splitdata = data.split("\\s+"); 
     int datalength=splitdata.length; 
     for (int i=0; i<datalength; i++) 
     { 
      if (splitdata[i].equals("1")) 
       splitdata[i]="**newline**"; 
      else if (splitdata[i].equals("8")) 
       splitdata[i]="**backspace**"; 
      else if (splitdata[i].equals("9")) 
       splitdata[i]="**tab**"; 
      else if (splitdata[i].equals("13")) 
       splitdata[i]="**enter**"; 
      else if (splitdata[i].equals("16")) 
       splitdata[i]="**shift**"; 
      else 
       System.out.println("oops!, no match for: " + splitdata[i]); 
     } 
     for(String s: splitdata) 
     { 
      System.out.println(s); 
     } 
    } 
} 

除此之外,如果你被允許集合,那麼我會 建議您使用HashMap中,而不是ATLEAST你可以擺脫這種醜陋 的if-else語句

+0

這裏是[demo](http://ideone.com/gAF4WY) – Arvind

+0

謝謝!這工作 – hwilco

+0

很高興幫助你:) – Arvind