首页 > 解决方案 > 试图创建一个密码。在我修复了输入字段的错误后,我遇到了变量无法解析的问题

问题描述

我已经评论了我收到错误消息的地方。在 promptUser 方法中,它要求我插入“VariableDeclaratorld”。关于 String s1= mc1.doEncryption(en); 的另一条错误消息 它说 en 不能解析为变量。

 import java.util.Scanner;

 public class MyCypher{
int cypher = 13;


public MyCypher(int cypher){
    this.cypher = cypher;
}

public int getCypher(){
    return cypher;
}
MyCypher mc1 = new MyCypher(cypher);
 //I am getting an error on the line below
public String promptUser(en){

   String en = sc.next().toLowerCase();
   Scanner sc= new Scanner(System.in);
   System.out.println("Enter the message:");
   return en;
  }


public String doEncryption(String s){
    
    
    String encrypted = "";
    char[] array = s.toCharArray();
    for (int i = 0; i < array.length; i++) {
    char shift = s.charAt(i);
    if (shift >= 'a' && shift <= 'z') {
    shift = (char) (shift + cypher);
    
    if (shift > 'z') {
        shift = (char)(shift - 'z' + 'a' - 1);
        encrypted += shift;
    }
    }
    else 
    encrypted += shift;
    }
    return encrypted;
    }
    //On the line below it says that en cannot be resolved to a variable
    
  String s1= mc1.doEncryption(en);

    
public String doDecryption(String s){
    String s1;
    
    System.out.println("Encrypted message: " + s1);
    String s2 = mc1.doDecryption(s1);
    System.out.println("Decrypted message: " + s2);
}

}

标签: javamethodsreturn

解决方案


在从控制台读取之前,您需要声明 Scanner,如下所示:

Scanner sc = new Scanner(System in):
String en = sc.next().toLowerCase();

推荐阅读