#java #methods #return
#java #методы #Возврат
Вопрос:
Я прокомментировал, где я получаю сообщения об ошибках. В методе promptUser он просит меня вставить «VariableDeclaratorld». В другом сообщении об ошибке для строки 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' amp;amp; 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:
Вам нужно объявить сканер перед чтением с консоли следующим образом:
Scanner sc = new Scanner(System in):
String en = sc.next().toLowerCase();