我已对收到错误消息的位置进行了评论。在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);
}
}
1条答案
按热度按时间new9mtju1#
在从控制台读取之前,您需要声明扫描仪,如下所示: