Java方法不断地给我:“引用;找不到符号“;
我正试图编写一个解密程序。我看了所有的地方,但我不知道是什么给了我错误。代码应该接受用户输入,接受一个数字(标记为key),然后将字符串中的所有字符按该数字移位。任何帮助都将不胜感激 这是我的密码:Java方法不断地给我:“引用;找不到符号“;,java,methods,compiler-errors,Java,Methods,Compiler Errors,我正试图编写一个解密程序。我看了所有的地方,但我不知道是什么给了我错误。代码应该接受用户输入,接受一个数字(标记为key),然后将字符串中的所有字符按该数字移位。任何帮助都将不胜感激 这是我的密码: import java.util.Scanner; import java.io.*; class Assignment3 { public static void decipher(int key, String scanInput) {
import java.util.Scanner;
import java.io.*;
class Assignment3 {
public static void decipher(int key, String scanInput)
{
for (int x = 0; x < scanInput.length(); x++) {
int letterDecode = abc.indexOf(scanInput.charAt(x)) + key;
deciphered = deciphered + abc.charAt(letterDecode);
}
System.out.println(deciphered);
}
public static void main(String[] args)
{
Scanner scan = new scanner(System.in);
String abc = "abcdefghijklmnopqrstuvdxyzABCDEFGHIJKLMNOPQRSTUPWXYZ";
String deciphered = "";
System.out.println("Please enter your code below");
String scanInput = scan.nextLine();
System.out.println("Please enter your key (Preferrable from 1-26");
int key = scan.nextInt();
decipher();
}
}
首先,您需要在decipher方法中声明abc和已解密的变量。 此外,不要忘记在方法中传递键和扫描输入变量(主方法的最后一行)。 请参见以下示例:
public static void decipher(int key, String scanInput)
{
String abc = "abcdefghijklmnopqrstuvdxyzABCDEFGHIJKLMNOPQRSTUPWXYZ";
String deciphered = "";
for (int x = 0; x < scanInput.length(); x++) {
int letterDecode = abc.indexOf(scanInput.charAt(x)) + key;
deciphered = deciphered + abc.charAt(letterDecode);
}
System.out.println(deciphered);
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your code below");
String scanInput = scan.nextLine();
System.out.println("Please enter your key (Preferrable from 1-26");
int key = scan.nextInt();
decipher(key, scanInput);
}
公共静态无效解密(int键,字符串输入)
{
字符串abc=“abcdefghijklmnopqrstuvdxyzABCDEFGHIJKLMNOPQRSTUPWXYZ”;
字符串破译=”;
对于(int x=0;x
代码似乎试图引用不在方法范围内的变量decipher
这甚至没有编译,我怀疑您是否有IndexOutOfBoundsException
。java区分大小写。新扫描仪(…)如果要使用扫描仪
,则
不起作用。将解密
方法的整个主体移动到主
方法的底部,即可完成。
public static void decipher(int key, String scanInput)
{
String abc = "abcdefghijklmnopqrstuvdxyzABCDEFGHIJKLMNOPQRSTUPWXYZ";
String deciphered = "";
for (int x = 0; x < scanInput.length(); x++) {
int letterDecode = abc.indexOf(scanInput.charAt(x)) + key;
deciphered = deciphered + abc.charAt(letterDecode);
}
System.out.println(deciphered);
}
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter your code below");
String scanInput = scan.nextLine();
System.out.println("Please enter your key (Preferrable from 1-26");
int key = scan.nextInt();
decipher(key, scanInput);
}