调用打印方法(数组和循环)

时间:2022-03-25 21:17:02
public class Encryption {
private static final int[] encrypt = {2, 9, 3, 4, 6, 8, 1, 0};
private static final int[] decrypt = new int[8];
private static final int minLength = 10;

String encrypt (String password)    {
    if(password.length()<minLength) {
        return password;

    }   else {
        char[] passArray = password.toCharArray();

        for (int i = 0; i < encrypt.length; i++)    {
            passArray[i] = (char) (passArray[i]);
            }
            return String.valueOf(passArray);
    }
}

String decrypt (String password)    {
    if (password.length()<minLength)    {
        return password;

    }   else {
        char[] arrayDecrypted = password.toCharArray();
        for (int i = 0; i < arrayDecrypted.length; i++) {
            arrayDecrypted[i] = (char) (arrayDecrypted[i]);
        }
        return String.valueOf(arrayDecrypted);
    }
}

//---------------------------------------------------------------------------

import csci130.*;

public class Driver {
public static void main(String args[]){

Encryption pass = new Encryption();

System.out.println("Please enter a password");
String name = KeyboardReader.readLine();

System.out.println("Encrypted Password:  " + pass.encrypt(name));
System.out.println("Decrypted Password:  " + pass.decrypt(name));

  }
}

When I try calling my method Encrypt and Decrypt and have the user type in a password it compiles, but only prints out what I typed in. Its suppose to permutate the password entered changing the index, and than change it back when decrypted. Above are two different classes Encryption & Driver. Any help would be greatly appreciated.

当我尝试调用我的方法Encrypt and Decrypt并让用户键入密码时,它会编译,但只打印出我输入的内容。它假设置换输入的密码更改索引,然后在解密时更改密码。以上是两个不同的类加密和驱动程序。任何帮助将不胜感激。

2 个解决方案

#1


3  

If you password is shorter than 10 your encrypt() returns it as is.

如果密码短于10,则encrypt()将按原样返回。

Otherwise it is iterating over characters of your password and does the following:

否则,它会迭代您的密码字符并执行以下操作:

passArray[i] = (char) (passArray[i]);

passArray [i] =(char)(passArray [i]);

I think it is obvious that this line has not effect.

我认为很明显这条线没有效果。

#2


1  

In your encrypt method, why do you do this?

在您的加密方法中,为什么这样做?

passArray[i] = (char) (passArray[i]);

What is your intention? (hint.. hint..)

你的意图是什么? (提示提示..)

#1


3  

If you password is shorter than 10 your encrypt() returns it as is.

如果密码短于10,则encrypt()将按原样返回。

Otherwise it is iterating over characters of your password and does the following:

否则,它会迭代您的密码字符并执行以下操作:

passArray[i] = (char) (passArray[i]);

passArray [i] =(char)(passArray [i]);

I think it is obvious that this line has not effect.

我认为很明显这条线没有效果。

#2


1  

In your encrypt method, why do you do this?

在您的加密方法中,为什么这样做?

passArray[i] = (char) (passArray[i]);

What is your intention? (hint.. hint..)

你的意图是什么? (提示提示..)