Java- 移动 ROT13 字符串
Java- shifting a ROT13 string
我想将 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 amount to shift by" 时,我最终得到了这个:
Enter your text to encrypt (!!! to quit): ERROR! String must not be empty!
Enter your text to encrypt (!!! to quit):
我不应该得到那个
shiftMessage()
就像 rot13()
,只是输入 n
而不是 13
:
private static String shiftMessage(String input, int n) {
String str = "";
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
if (ch >= 'A' && ch <= 'Z')
{
ch = (char) (ch + n);
if (ch > 'Z')
{
ch = (char)(ch - 26);
}
}
else if (ch >= 'a' && ch <= 'z')
{
ch = (char)(ch + n);
if (ch > 'z')
{
ch = (char)(ch - 26);
}
}
str = str + ch;
}
return str;
}
现在,当我从 main()
调用 shiftMessage()
而不是 rot13()
时,我可以:
Enter your text to encrypt (!!! to quit): abcdefghijklmnopqrstuvwxyz
Enter amount to shift text by: 1
+---------------------------------------+
| ORIGINAL: abcdefghijklmnopqrstuvwxyz |
| ENCRYPTED: bcdefghijklmnopqrstuvwxyza |
+---------------------------------------+
其他问题:
你说的ERROR! String must not be empty!
是Scanner.nextInt()
的经典。 nextInt()
只读取数字,不读取后面的换行符。因此,当您随后执行 inScanner.nextLine()
时,它会读取换行符和 returns 空字符串。我相信解决方案是在 nextInt()
之后调用 nextLine()
以摆脱换行符。
最后,您无法提前知道用户只会输入一次错误的班次金额。我能做到:
Enter your text to encrypt (!!! to quit): abc
Enter amount to shift text by: -3
ERROR! Shift must be non-negative!
Enter amount to shift text by: -3
+----------------+
| ORIGINAL: abc |
| ENCRYPTED: ^_` |
+----------------+
我想将 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 amount to shift by" 时,我最终得到了这个:
Enter your text to encrypt (!!! to quit): ERROR! String must not be empty!
Enter your text to encrypt (!!! to quit):
我不应该得到那个
shiftMessage()
就像 rot13()
,只是输入 n
而不是 13
:
private static String shiftMessage(String input, int n) {
String str = "";
for (int i = 0; i < input.length(); i++)
{
char ch = input.charAt(i);
if (ch >= 'A' && ch <= 'Z')
{
ch = (char) (ch + n);
if (ch > 'Z')
{
ch = (char)(ch - 26);
}
}
else if (ch >= 'a' && ch <= 'z')
{
ch = (char)(ch + n);
if (ch > 'z')
{
ch = (char)(ch - 26);
}
}
str = str + ch;
}
return str;
}
现在,当我从 main()
调用 shiftMessage()
而不是 rot13()
时,我可以:
Enter your text to encrypt (!!! to quit): abcdefghijklmnopqrstuvwxyz
Enter amount to shift text by: 1
+---------------------------------------+
| ORIGINAL: abcdefghijklmnopqrstuvwxyz |
| ENCRYPTED: bcdefghijklmnopqrstuvwxyza |
+---------------------------------------+
其他问题:
你说的ERROR! String must not be empty!
是Scanner.nextInt()
的经典。 nextInt()
只读取数字,不读取后面的换行符。因此,当您随后执行 inScanner.nextLine()
时,它会读取换行符和 returns 空字符串。我相信解决方案是在 nextInt()
之后调用 nextLine()
以摆脱换行符。
最后,您无法提前知道用户只会输入一次错误的班次金额。我能做到:
Enter your text to encrypt (!!! to quit): abc
Enter amount to shift text by: -3
ERROR! Shift must be non-negative!
Enter amount to shift text by: -3
+----------------+
| ORIGINAL: abc |
| ENCRYPTED: ^_` |
+----------------+