首页 > 解决方案 > 错误:找不到符号:方法 rotate()

问题描述

我必须编写一个构造函数,它接受一个 27 个字母的序列和一个“旋转”字符序列的字符,直到序列中的第一个字符与给定的字符匹配。然后将生成的字符串作为字段保存在类中。

例如:如果我要使用#GNUAHOVBIPWCJQXDKRYELSZFMT 和 U,我希望构造函数给我UAHOVBIPWCJQXDKRYELSZFMT#GN

我的构造函数找不到我写的旋转方法。我的其他方法之一 peek() 方法也有类似的问题,所以我删除了该方法并使用了构造函数中的代码,但我想练习良好的 OOP 原则,所以我不想在这里做同样的事情.

public class Rotor{
    private String rotor;

    public Rotor(String rotor, char top){
        this.rotor = rotor;
        while(this.rotor.charAt(0)!=top){
            rotor.rotate();
        }
    }
    //rotates rotor one click clockwise
    public void rotate(){
        this.rotor = this.rotor.substring(this.rotor.length()-1, this.rotor.length()) + this.rotor.substring(0, this.rotor.length()-1);
    }

    public char peek(){
        return rotor.charAt(0);
    }
    public String toString(){
        return rotor;
    }
    public static void main(String[] args){
        Rotor rotor = new Rotor("#GNUAHOVBIPWCJQXDKRYELSZFMT", 'U');
        System.out.println(rotor.toString());
        
    }
}

    Rotor.java:7: error: cannot find symbol
                rotor.rotate();
                     ^
      symbol:   method rotate()
      location: variable rotor of type String
    1 error

标签: enigma2

解决方案


推荐阅读