我一直收到错误:"无法对类型为Encrypt"
的非静态方法编码(字符串)进行静态引用它出现在这一行: encWord = encode(plaintext);
谁能告诉我发生了什么?我看过类似的问题,继续尝试不同的事情,但没有运气。希望我的代码现在不是太乱了。
public class Encrypt implements MessageEncode {
public static void main(String[] args) {
// TODO Auto-generated method stub
String choice;
String ans;
System.out.print("Hi\n");
do{
String plaintext;
choice = "";
ans = "";
String encWord;
Scanner keyboard = new Scanner(System.in);
System.out.print("Which word would you wish to encode or decode?: ");
plaintext = keyboard.next();
System.out.print("How far would you like the letters shifted?: ");
shift = keyboard.nextInt();
encWord = encode(plaintext);
System.out.print(plaintext+"\n");
while(!choice.equals("y") && !choice.equals("n")){
System.out.print("Would you like to continue? (y or n): ");
choice = keyboard.next();
}
if(choice.equals("y")){
ans = "y";
System.out.println("\nOnce more...");
} else{
ans = "n";
}
}while(ans == "y");
}
private static int shift;
public Encrypt(int shift){
Encrypt.shift = shift;
}
@Override
public String encode(String plaintext) {
StringBuffer newtext = new StringBuffer();
newtext = new StringBuffer(plaintext);
for(int i=0; i < newtext.length(); i++){
for(int n=0; n < 25; n++){
newtext = shifter(newtext, i, n);
}
}
return newtext.toString();
}
public StringBuffer shifter(StringBuffer plaintext, int i, int n) {
char[] letters =
{ 'a','b','c','d','e','f','g','h','i','j','k','l',
'm','n','o','p','q','r','s','t','u','v','w','x','y','z'
};
StringBuffer newtext = new StringBuffer();
newtext = new StringBuffer(plaintext);
if(newtext.charAt(i) == letters[n]){
n += shift;
newtext.setCharAt(i, letters[n]);
}
return newtext;
}
}
-------------------------------------
//Also, it has an interface:
public interface MessageEncode {
public abstract String encode(String plaintext);
}