java - 试图创建一个密码。在我修复了输入字段的错误后,我遇到了变量无法解析的问题
问题描述
我已经评论了我收到错误消息的地方。在 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);
}
}
解决方案
在从控制台读取之前,您需要声明 Scanner,如下所示:
Scanner sc = new Scanner(System in):
String en = sc.next().toLowerCase();
推荐阅读
- kubernetes - k3s ctr 图像导入到不同的目录
- c# - 如何验证字符串、整数、日期时间
- java - 输入的数据没有被存储,也没有显示错误
- apache - 如何将 activiti 工作流引擎与 Vertx 集成?
- drupal - 如何将 Drush 命令发送到 Pantheon
- django - 如何使用 django-allauth 将一个社交帐户映射到多个用户帐户
- c++ - 当所有函数都在标头中定义时,“dllexport”C++ 类是否有效?
- java - 如何模拟扩展另一个接口的接口
- java - 在 Spring Boot 启动时将数据加载到 Ehcache 3
- azure-devops - 错误:指定的资源名称包含无效字符。错误代码:无效资源名称