我的代码看起来是正确的,我看不出它有什么问题,但程序输出不正确(代码后的特异性):
两个类:Test.java
和 Key.java
测试.java:
public class Test{
public static void main(String[] args) {
Key k1,k2;
k1 = new Key(5);
k2 = new Key(15);
System.out.println(k1.encode('a')); // expected output 'f'
System.out.println(k2.encode('a')); // expected output 'p'
System.out.println(k1.encode('7')); // expected output '2'
}
Key.java:
class Key{
private int value;
Key(int value) {}
public char encode(char c){
if (isValidKey(value) != true) {
return '.';
} else {
int code = (int) c;
code = code + value;
c = (char) code;
return c;
}
}
当我对输入 a、a 和 2
运行测试时,它只是返回 ... a a 和 2
(而不是预期的 f p 和 2
)。
最佳答案
值始终为 0。修复您的构造函数。这个
Key(int value) {
}
应该是这个
Key(int value) {
// Store the value in private this.value.
this.value = value;
}
关于java - 不会对设置的字符进行加密吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20247287/