2013-02-24 88 views
-2

因此,对于我的生活,我无法弄清楚为什么for循环加密没有进入!我已经放置了一些打印语句,以确认它没有进入理论上它应该打印im内部3次,而是它进入第一个条件的内部,但是然后不进入第一个for循环(由我)不进入循环

//method for Ceaser encryption takes in a string in plain text or encrypted form, the key, and mode option 
public static String ceaser(String a, int x, int T){ 

    System.out.println(a); 
    System.out.println(x); 
    System.out.println(T); 
    String full = "abcdefghijklmnopqrstuvwxyz"; 
    String output =""; 
    //used for type matching for concat method 
    String convertChar=""; 
    char[] alpha = full.toCharArray(); 

    //used to find the inital value (index of the plain text letter in the alphabet 
    int position = 0; 


    //selecting encryption 
    if(T==1){ 
     System.out.println("im inside"); 
     for (int i=0; i>a.length(); i++){ 
      System.out.println("im inside"); 
      for (int l=0; l>full.length(); l++){ 
       System.out.println("im inside"); 
       System.out.println(a.charAt(i)); 

       if (a.charAt(i) == full.charAt(l)) 
        System.out.println(full.charAt(l)); 
        position = l; 
        System.out.println(position); 

      } 
       //Handling the circular property of the shift 
       if ((position + x) > 25){ 
        System.out.println(convertChar); 
        convertChar = Character.toString(full.charAt((25-position)-x)); 
        System.out.println(convertChar); 
        output.concat(convertChar); 
       } 
      else{ 
       System.out.println(convertChar); 
       convertChar = Character.toString(full.charAt(position + x)); 
       System.out.println(convertChar); 
       output.concat(convertChar); 
      } 
     } 
    } 
    // Selecting decryption 
    else { 
     for (int u=0; u>a.length(); u++){ 

      for (int b=0; b>full.length(); b++){ 
       if (a.charAt(u) == full.charAt(b)) 
        position = b; 

       //handling circular property 
       if((position - x) < 0){ 
        convertChar = Character.toString(full.charAt(25-(x-position))); 
        output.concat(convertChar); 
       } 
       //applying inverse shift 
       else{ 

        //try printng out convert char for debug 
        convertChar = Character.toString(full.charAt(position - x)); 
        output.concat(convertChar); 
       } 

      } 
     } 
    } 
    //displaying the output 
    System.out.println(output); 
    System.out.println("death of ceaser"); 
    return output; 
} 

节目电流输出为:

Please select the type of encryption you would like performed enter 1 for Ceaser, 2 for vigenere, or 3 for matrix transposition 
1 
Please enter 1 for encryption or 2 for decryption 
1 
Please input the string you want encrypted or decrypted: 
yuck 
Enter the amount to shift by, aka the Key: 
2 
yuck 
2 
1 
im inside 

death of ceaser 

回答

5

使用小于<表示你的循环结束的条件。替换:

for (int i=0; i>a.length(); i++){ 

for (int i=0; i < a.length(); i++){ 

同applys这里:

for (int l=0; l>full.length(); l++){ 

变化

for (int l=0; l < full.length(); l++){ 
+0

oh man ... facepalm!谢谢深夜,每次都会这样做! >< – 2013-02-24 15:12:03

2
for (int i=0; i>a.length(); i++){ 
      System.out.println("im inside"); 
      for (int l=0; l>full.length(); l++){ 

我客串如果你与错误的操作员进行比较,是不是应该是i < a.length()l < full.length()

1

这是因为你在零有

i>a.length() 

i开始,所以它不能比你的字符串,长度更大不管该字符串多么短暂。

您需要更改条件为i < a.length()