好吧,我的任务是将字符串转换为其各自的 unicode 整数,根据所需的加密(左或右以及多少个空格)进行移位。我能够很好地解决这部分问题。但是然后我需要输入移位的 unicode 字符串并将其转换回输入的原始字符串。这是我的代码。我不知道如何将 unicode 字符串转换回原始字符串。**注意 - 我只允许使用整数和字符串。
import java.util.Scanner;
import java.lang.String;
import java.lang.Character;
public class CSCD210_HW2
{
public static void main(String [] args){
Scanner input = new Scanner(System.in);
String str, str1 = "";
String encrypt, decrypt = "";
int i = 0;
int i1 = (int)0;
System.out.printf("Please enter a string:");
str = input.nextLine();
System.out.printf("\nPlease enter encryption format - \'left\' or \'right\'" +
" \"space\" number of spaces:");
encrypt = input.nextLine();
int length = str.length();
String spaces = encrypt.substring(encrypt.lastIndexOf(' ') + 1);
Integer x = Integer.valueOf(spaces);
//encrypt
if (encrypt.startsWith("l")){
while (i < length){
int uni = (int)(str.charAt(i++));
char uni1 = (char)uni;
int result = uni + x;
System.out.print(result + " ");}}
else if (encrypt.startsWith("r")){
while (i < length){
int uni = (int)(str.charAt(i++));
char uni1 = (char)uni;
int result = uni - x;
System.out.print(result + " ");}}
//decrypt
System.out.printf("\nPlease enter encrypted string:");
str1 = input.nextLine();
System.out.printf("\n\'left\' or \'right\' \"space\" number of spaces:");
decrypt = input.nextLine();
int length1 = str1.length();
String spaces1 = decrypt.substring(decrypt.lastIndexOf(' ') + 1);
Integer y = Integer.valueOf(spaces1);
if (decrypt.startsWith("l")){
while (i < length1){
char word = (char)(str1.charAt(i++));
int result = word + y;
System.out.print(result);}}
else if (decrypt.startsWith("r")){
while (i < length1){
char word = (char)(str1.charAt(i++));
int result = word - y;
System.out.print(result);}}
}
}