Java 如何让我的凯撒密码读取菜单选项中的空格
我的程序中有一个菜单选项,允许用户更改消息(案例3:),但该选项会一直打印错误。我有sc.nextLine();现在输入的,我认为会工作,但错误只是不断弹出。我拿出那根线,让它正好在sc.next()上;这允许它打印,但只有在没有空格的情况下。输入一个空格,将弹出一个错误。请看案例3: 密码包Java 如何让我的凯撒密码读取菜单选项中的空格,java,Java,我的程序中有一个菜单选项,允许用户更改消息(案例3:),但该选项会一直打印错误。我有sc.nextLine();现在输入的,我认为会工作,但错误只是不断弹出。我拿出那根线,让它正好在sc.next()上;这允许它打印,但只有在没有空格的情况下。输入一个空格,将弹出一个错误。请看案例3: 密码包 import java.util.Scanner; public class CaeserCipher { public static String encrypt(String Salad
import java.util.Scanner;
public class CaeserCipher {
public static String encrypt(String Salad, int CipherKeyLength)
{
String EncryptedMessage = "";
for (int i = 0; i < Salad.length(); i++) {
int c = Salad.charAt(i);
if (Character.isUpperCase(c)) {
c = c + (CipherKeyLength % 26);
if (c > 'Z')
c = c - 26;
} else if (Character.isLowerCase(c)) {
c = c + (CipherKeyLength % 26);
if (c > 'z')
c = c - 26;
}
EncryptedMessage = EncryptedMessage + (char) c;
}
return EncryptedMessage;
}
public static String decrypt(String Salad, int CipherKeyLength) {
String DecryptedMessage = "";
for (int i = 0; i < Salad.length(); i++) {
int c = Salad.charAt(i);
if (Character.isUpperCase(c)) {
c = c - (CipherKeyLength % 26);
if (c < 'A')
c = c + 26;
} else if (Character.isLowerCase(c)) {
c = c - (CipherKeyLength % 26);
if (c < 'a')
c = c + 26;
}
DecryptedMessage = DecryptedMessage + (char) c;
}
return DecryptedMessage;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String Salad;
int CipherKeyLength;
int Menu;
System.out.println("Please Enter The Message You Wish To
Encrypt Or Decrypt:");
Salad = sc.nextLine();
System.out.println("\nPlease Enter Your Encryption Key The
Number of Places You Want It To Shift (1-25): ");
CipherKeyLength = sc.nextInt();
do {
System.out.println(
"\n--------------MENU--------------\n\n1 - ALLOWS
YOU TO ENCRYPT YOUR MESSAGE\n2 - ALLOWS YOU TO DECRYPT YOUR
MESSAGE\n3 - ALLOWS YOU TO CHANGE YOUR ORIGINAL MESSAGE OR JUST START
A NEW ONE ALTOGETHER\n4 - ALLOWS YOU TO CHANGE YOUR ENCRYPTION KEY\n5
- EXIT PROGRAM\n\n--PLEASE MAKE A SELECTION BY CHOOSING ONE OF THE
ABOVE NUMBER OPTIONS--");
Menu = sc.nextInt();
switch (Menu) {
case 1:
System.out.println("\nYour Encrypted Message
Reads...\n\n" + encrypt(Salad, CipherKeyLength));
break;
case 2:
System.out.println("Your Decrypted Message
Reads...\n\n"
+ decrypt(encrypt(Salad, CipherKeyLength),
CipherKeyLength));
break;
case 3:
System.out.println("Please Enter The Message You
Wish To Encrypt Or Decrypt:");
Salad = sc.nextLine();
System.out.println(
"\nPlease Enter Your Encryption Key The
Number of Places You Want It To Shift (1-25): ");
CipherKeyLength = sc.nextInt();
break;
case 4:
System.out.println(
"\nPlease Enter Your Encryption Key The
Number of Places You Want It To Shift (1-25): ");
CipherKeyLength = sc.nextInt();
break;
case 5:
System.out.println("Exiting Program...");
System.exit(0);
default:
System.err.println(Menu + " Is Not A Valid Option.
Please Try Again.");
}
} while (Menu != 5);
}
}
import java.util.Scanner;
公共类CaeserCipher{
公共静态字符串加密(字符串色拉,int-CipherKeyLength)
{
字符串EncryptedMessage=“”;
对于(int i=0;i‘Z’)
c=c-26;
}else if(字符.isLowerCase(c)){
c=c+(CipherKeyLength%26);
如果(c>‘z’)
c=c-26;
}
EncryptedMessage=EncryptedMessage+(char)c;
}
返回加密消息;
}
公共静态字符串解密(字符串沙拉,int-CipherKeyLength){
字符串DecryptedMessage=“”;
对于(int i=0;i
此解决方案可能会有所帮助
这解决了您的标题问题:从输入中读取空格
Reader r = new StringReader(Salad);
read(r);
...
private static void read(Reader r) throws IOException {
int readChar;
while ((readChar = r.read()) != -1) {
char c = (char) readChar;
if (c == ' '){
//if character is a space, do something
}
}
}
在您的
开关的案例3
中,以前似乎是,CipherKeyLength=sc.nextInt()代码>在输入中留下一个换行符。因此,该换行符被第一个
sc.nextLine()
使用,并跳过第一个输入。要解决此问题,请添加sc.nextLine()代码>之前色拉=sc.nextLine()代码>:
此解决方案可能会有所帮助
这解决了您的标题问题:从输入中读取空格
Reader r = new StringReader(Salad);
read(r);
...
private static void read(Reader r) throws IOException {
int readChar;
while ((readChar = r.read()) != -1) {
char c = (char) readChar;
if (c == ' '){
//if character is a space, do something
}
}
}
在您的开关的案例3
中,以前似乎是,CipherKeyLength=sc.nextInt()代码>在输入中留下一个换行符。因此,该换行符被第一个
sc.nextLine()
使用,并跳过第一个输入。要解决此问题,请添加sc.nextLine()代码>之前色拉=sc.nextLine()代码>:
它起作用了,我知道它跳过了第一个输入,但是你能解释一下为什么sc.nextLine();上面的沙拉解决了这个问题。既然已经说明了,为什么还要用它呢。键入CipherKeyLength
value后,光标与输入保持在同一行上;但在键入下一行中找到的sala
value后,光标无法读取该值,将读取CipherKeyLength
value后的空白。因此,一个额外的sc.nextLine()
将使光标转到下一行以读取sala
值。它起作用了,我知道它跳过了第一个输入,但您能解释一下为什么sc.nextLine();上面的沙拉解决了这个问题。既然已经说明了,为什么还要用它呢。键入CipherKeyLength
value后,光标与输入保持在同一行上;但在键入下一行中找到的sala
value后,光标无法读取该值,将读取