有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java方法一直给我:“找不到符号”

我正试图编写一个解密程序。我看了所有的地方,但我不知道是什么给了我错误。代码应该接受用户输入,接受一个数字(标记为key),然后将字符串中的所有字符按该数字移位。任何帮助都将不胜感激

这是我的密码:

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();        
           
    }
}

以下是错误:

     Assignment3.java:21: error: cannot find symbol
                int letterDecode = abc.indexOf(scanInput.charAt(x)) + key;
                                   ^
      symbol:   variable abc
      location: class Assignment3
    Assignment3.java:22: error: cannot find symbol
                deciphered = deciphered + abc.charAt(letterDecode); 
                ^
      symbol:   variable deciphered
      location: class Assignment3
    Assignment3.java:22: error: cannot find symbol
                deciphered = deciphered + abc.charAt(letterDecode); 
                             ^
      symbol:   variable deciphered
      location: class Assignment3
    Assignment3.java:22: error: cannot find symbol
                deciphered = deciphered + abc.charAt(letterDecode); 
                                          ^
      symbol:   variable abc
      location: class Assignment3
    Assignment3.java:25: error: cannot find symbol
                System.out.println(deciphered);
                                   ^
      symbol:   variable deciphered
      location: class Assignment3
    Assignment3.java:32: error: cannot find symbol
          Scanner scan = new scanner(System.in);
                             ^
      symbol:   class scanner
      location: class Assignment3
    Assignment3.java:43: error: method decipher in class Assignment3 cannot be applied to given types;
          decipher();
          ^
      required: int,String
      found: no arguments
      reason: actual and formal argument lists differ in length
    7 errors

共 (1) 个答案

  1. # 1 楼答案

    首先,您需要在解密方法中声明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);
    }