我想按用户输入的量移动 ROT13 字符串。这是示例输出:
Enter your text to encrypt (!!! to quit): abcdefghijklmnopqrstuvwxyz
Enter amount to shift text by: 3
ORIGINAL: abcdefghijklmnopqrstuvwxyz
ENCRYPTED: defghijklmnopqrstuvwxyzabc
用户必须提供 0 到 26 之间的移位值。如果用户超出此范围,程序应强制用户输入有效值:
Enter your text to encrypt (!!! to quit): the quick brown fox jumped over the lazy dog
Enter amount to shift text by: -3
ERROR! Shift must be non-negative!
Enter amount to shift text by: 33
ERROR! Shift must be no more than 26!
Enter amount to shift text by: 2
输入要加密的文本(!!!退出):!!!再见!
这是我编写的代码:
import java.util.Scanner;
public class Lab08b
{
public static void main(String[] args)
{
Scanner sc = new Scanner (System.in);
String input = getText(sc);
int shift = getShift(sc);
if (input.equals("!!!"))
{
System.out.println("Goodbye!");
}
while (!input.equals("!!!"))
{
String encoded = rot13(input);
shiftMessage(input, shift);
displayResults(input, encoded);
input = getText(sc);
if (input.equals("!!!"))
{
System.out.println("Goodbye!");
}
}
}
private static int getShift (Scanner inScanner)
{
System.out.print("Enter amount to shift text by: ");
int shift = inScanner.nextInt();
if (shift < 0)
{
System.out.println("ERROR! Shift must be non-negative!");
System.out.print("Enter amount to shift text by: ");
shift = inScanner.nextInt();
}
else if (shift > 26)
{
System.out.println("ERROR! Shift must be non-negative!");
System.out.print("Enter amount to shift text by: ");
shift = inScanner.nextInt();
}
return shift;
}
private static String getText(Scanner inScanner)
{
System.out.print("Enter your text to encrypt (!!! to quit): ");
String original = inScanner.nextLine();
while (original.equals(""))
{
System.out.println("ERROR! String must not be empty!");
System.out.print("Enter your text to encrypt (!!! to quit): ");
original = inScanner.nextLine();
}
return original;
}
private static String rot13(String input)
{
String str = "";
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
if (ch >= 'A' && ch <= 'Z')
{
ch = (char) (ch + 13);
if (ch > 'Z')
{
ch = (char)(ch - 26);
}
}
else if (ch >= 'a' && ch <= 'z')
{
ch = (char)(ch + 13);
if (ch > 'z')
{
ch = (char)(ch - 26);
}
}
str = str + ch;
}
return str;
}
private static void displayResults(String inText, String encText)
{
System.out.print("+");
for (int i = 13 + encText.length(); i > 0; i--)
{
System.out.print("-");
}
System.out.println("+");
System.out.println("| ORIGINAL: " + inText + " |");
System.out.println("| ENCRYPTED: " + encText + " |");
System.out.print("+");
for(int i = 13 + encText.length(); i > 0; i--){
System.out.print("-");
}
System.out.println("+");
}
private static String shiftMessage(String input, int n)
{
for (int i = 0; i < input.length(); i++)
{
input = input.charAt(input.length()- n) + input.substring(0,input.length());
}
return input;
}
}
更新:这个代码是我的进步。实际转移仍然不起作用 http://pastebin.com/v2T1fxEj
我让它打印出 ROT13 加密和所有内容,我只是不知道如何转移它。
另外,当我尝试在 String input = getText(sc) 下面的主要方法中添加“输入要移位的数量”时,我最终得到了这个:
Enter your text to encrypt (!!! to quit): ERROR! String must not be empty!
Enter your text to encrypt (!!! to quit):
我不应该得到那个