尝试创建密码我有麻烦的变量变得无法解决后,我修复了我的输入字段的错误

n1bvdmb6  于 2021-07-08  发布在  Java
关注(0)|答案(1)|浏览(319)

我已对收到错误消息的位置进行了评论。在promptuser方法中,它要求我在字符串s1=mc1.doencryption(en)的另一个错误消息中插入“variabledeclaratorld.”;它说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);
}

}

new9mtju

new9mtju1#

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

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

相关问题