我在这里遇到了一些代码,我想做的是将字符串转换为其 ASCII 值,从中减去 30,然后再转换回字符串。
E.g. Enter - hello
Convert to - 104 101 108 108 111
Subtract - 74 71 78 78 81
display - JGNNQ
代码:
import javax.swing.*;
public class practice {
public static void main (String[] args) {
String enc = "";
String encmsg = "";
String msg = JOptionPane.showInputDialog("Enter your message");
int len = msg.length();
for (int i = 0; i< len ; i++) {
char cur = msg.charAt(i);
int val = (int) cur;
val = val -32;
enc = "" + val;
encmsg = encmsg + enc;
}
JOptionPane.showMessageDialog(null, encmsg);
}
}
提前致谢