简体   繁体   中英

encrypting numbers in java using a method

I'm currently working on a project, and I don't understand why my second method doesn't work. I'm attempting to change the numbers entered by the user to another number. Each number should be added +7, then divided by 10. The first and third digit are supposed to be swapped, as are the second and fourth. It is supposed to accept four digits, and work with each one individually. I'm not sure how to get the results my professor is asking for. He said the result of 1234 should be 0189, but instead I keep getting 13 no matter how I swap them... I have attempted to modify the method to separate the digits, then perform the math required afterwards, but now the result is 14... I also don't understand how to have the compiler return a zero at the beginning of the number, or to keep it from dropping the zero at the beginning of the number when it is returned. Any help would be appreciated, but please don't hate on me for not understanding this assignment.

import java.util.*;

public class SeayJ_program2
{
  static Scanner input = new Scanner (System.in);
  public static void main (String [] args)
  {


  int num = getnum(); 

  System.out.println(encrypt(num));

  }
  // methods are here  
  public static int getnum()
  {
  int num;

     System.out.println("Please enter a four digit number");
     num = input.nextInt();

  return num;    
  }


  public static int encrypt(int num)
  {
    int digit1, digit2, digit3, digit4;
    digit4 = num % 10;
    num = num / 10;
    digit4 = (num + 7) / 10;
    digit3 = num % 10;
    num = num / 10;
    digit3 = (num + 7) / 10;
    digit2 = num % 10;
    num = num / 10;
    digit2 = (num + 7) / 10;
    digit1 = num % 10;
    num = num / 10;
    digit1 = (num + 7) / 10;
    return digit3 + digit4 + digit1 + digit2;
  }

  }  

This should work for getting the encrypted number. Think how to handle the missing zero(s)

public static int encrypt(int number){
    int temp = number;
    int digit4 = (temp + 7) % 10;
    temp = temp / 10;
    int digit3 = (temp + 7) % 10;
    temp = temp / 10;
    int digit2 = (temp + 7) % 10;
    temp = temp / 10;
    int digit1 = (temp + 7) % 10;
    return (digit3 * 1000 + digit4 * 100 + digit1 * 10 + digit2);

}

您将在其中使用Jasypt ,您可以调用mask和unmask

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM