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

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

我已对收到错误消息的位置进行了评论。在promptuser方法中,它要求我在字符串s1=mc1.doencryption(en)的另一个错误消息中插入“variabledeclaratorld.”;它说en不能解析为一个变量。

  1. import java.util.Scanner;
  2. public class MyCypher{
  3. int cypher = 13;
  4. public MyCypher(int cypher){
  5. this.cypher = cypher;
  6. }
  7. public int getCypher(){
  8. return cypher;
  9. }
  10. MyCypher mc1 = new MyCypher(cypher);
  11. //I am getting an error on the line below
  12. public String promptUser(en){
  13. String en = sc.next().toLowerCase();
  14. Scanner sc= new Scanner(System.in);
  15. System.out.println("Enter the message:");
  16. return en;
  17. }
  18. public String doEncryption(String s){
  19. String encrypted = "";
  20. char[] array = s.toCharArray();
  21. for (int i = 0; i < array.length; i++) {
  22. char shift = s.charAt(i);
  23. if (shift >= 'a' && shift <= 'z') {
  24. shift = (char) (shift + cypher);
  25. if (shift > 'z') {
  26. shift = (char)(shift - 'z' + 'a' - 1);
  27. encrypted += shift;
  28. }
  29. }
  30. else
  31. encrypted += shift;
  32. }
  33. return encrypted;
  34. }
  35. //On the line below it says that en cannot be resolved to a variable
  36. String s1= mc1.doEncryption(en);
  37. public String doDecryption(String s){
  38. String s1;
  39. System.out.println("Encrypted message: " + s1);
  40. String s2 = mc1.doDecryption(s1);
  41. System.out.println("Decrypted message: " + s2);
  42. }

}

new9mtju

new9mtju1#

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

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

相关问题