我收到了错误的整数。而不是7777我应该得到0189

时间:2014-10-27 13:18:09

标签: java encryption

import java.util.*;    
public class LNFI_LNFI_program2 {    
static int globnum;    
static Scanner console = new Scanner (System.in);    
 public static void main(String[] args) {    
  int globnum2 = globnum;    
  getnum();    
  encrypt(globnum2);      
 }

 // encrypt methods    
 public static int encrypt(int num)    
 {    
  num = globnum;    
   int firstDigit, secondDigit, thirdDigit, fourthDigit,  temp;      
   firstDigit = num / 1000 % 10;    
         secondDigit = num / 100 % 10;    
         thirdDigit = num / 10 % 10;    
         fourthDigit = num % 10;      
         firstDigit = (firstDigit + 7) % 10;    
         secondDigit = (secondDigit + 7) % 10;    
         thirdDigit = (thirdDigit + 7) %10;
         fourthDigit = (fourthDigit + 7) % 10;      
         temp = firstDigit;    
         firstDigit = thirdDigit;    
         thirdDigit = temp;      
         temp = secondDigit;    
         secondDigit = fourthDigit;    
         fourthDigit = temp;             
         System.out.printf("the encrypted number is %d%d%d%d\n",    
                 firstDigit, secondDigit, thirdDigit, fourthDigit);             
   return num;    
 }     

 // getnum    
 public static int getnum()    
    {    
      int numentered;          
      System.out.println("Please enter a number");    
      numentered = console.nextInt();          
      return numentered;    
    }    
}

这是返回值,我收到

  

请输入数字
  1234
  加密号码是7777

1 个答案:

答案 0 :(得分:0)

您覆盖传递给encrypt方法的号码:

num = globnum;

globnum默认为0。

当然,即使你没有覆盖它,你也会将同样的0传递给你的encrypt方法。

您永远不会使用getnum()方法的输出。

因此0被加密为7777。

将您改为:

 public static void main(String[] args) {       
     encrypt(getnum());      
 }

并删除encrypt的第一行。