运行此程序时,它不会打印加密或解密输出,并打印用户提示输入消息,但实际上不允许输入。
import java.util.*;
public class CaesarShiftTester
{
public static void main(String [] args)
{
String alphabet[] = {"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z"};
Scanner in = new Scanner(System.in);
System.out.print("Do you wish to:\n[1]Encrypt\n[2]Decrypt\n\n");
while (!in.hasNextInt())
{
System.out.println("\nPlease enter an integer value: ");
in.nextLine();
}
int decision = in.nextInt();
System.out.println();
if(decision == 1)
{
System.out.println("Enter an integer value between 1-25 by which you desire to shift your message: ");
while (!in.hasNextInt())
{
System.out.print("\nPlease enter an integer value");
in.nextLine();
}
int shift = in.nextInt();
if(shift > 25 || shift < 1)
{
shift = 0;
}
System.out.println();
System.out.println("Enter a phrase you wish to encrypt: ");
while (!in.hasNextLine())
{
in.nextLine();
}
String entry= in.nextLine();
entry = entry.toLowerCase();
System.out.println(CaesarShiftEncryption.encrypt(alphabet, entry, shift));
}
else
{
System.out.println("Enter an integer value between 1-25 by which you desire to shift your message: ");
while (!in.hasNextInt())
{
System.out.print("\nPlease enter an integer value: ");
in.nextLine();
}
int shift = in.nextInt();
if(shift > 25 || shift < 1)
{
shift = 0;
}
System.out.println();
System.out.println("Enter a phrase you wish to decrypt: ");
while (!in.hasNextLine())
{
in.nextLine();
}
String entry= in.nextLine();
entry = entry.toLowerCase();
System.out.println(CaesarShiftDecryption.decrypt(alphabet, entry, shift));
}
}
}
这个类调用这两个类:
public class CaesarShiftDecryption
{
public static String decrypt(String str[], String in, int shift)
{
String decryption = "";
for(int i = 0; i < in.length(); i++)
{
int charval = in.charAt(i);
if(charval-shift < 97 && charval < 123)
{
charval += 26;
}
if(charval > 96)
{
charval -= shift;
String token = str[charval-97];
decryption += token;
}
else
{
decryption += in.charAt(i);
}
charval = 0;
}
return decryption;
}
}
public class CaesarShiftEncryption
{
public static String encrypt(String str[], String in, int shift)
{
String encryption = "";
for(int i = 0; i < in.length(); i++)
{
int charval = in.charAt(i);
if(charval+shift > 122 && charval < 123)
{
charval -= 26;
}
if(charval > 96)
{
charval += shift;
String token = str[charval-97];
encryption += token;
}
else
{
encryption += in.charAt(i);
}
charval = 0;
}
return encryption;
}
}
但是,我构建了一个额外的扫描仪对象,我的测试人员代码如下所示:
import java.util.*;
public class CaesarShiftTester
{
public static void main(String [] args)
{
String alphabet[] = {"a" , "b" , "c" , "d" , "e" , "f" , "g" , "h" , "i" , "j" , "k" , "l" , "m" , "n" , "o" , "p" , "q" , "r" , "s" , "t" , "u" , "v" , "w" , "x" , "y" , "z"};
Scanner in = new Scanner(System.in);
System.out.print("Do you wish to:\n[1]Encrypt\n[2]Decrypt\n\n");
while (!in.hasNextInt())
{
System.out.println("\nPlease enter an integer value: ");
in.nextLine();
}
int decision = in.nextInt();
System.out.println();
if(decision == 1)
{
Scanner in1 = new Scanner(System.in);
System.out.println("Enter an integer value between 1-25 by which you desire to shift your message: ");
while (!in.hasNextInt())
{
System.out.print("\nPlease enter an integer value");
in.nextLine();
}
int shift = in.nextInt();
if(shift > 25 || shift < 1)
{
shift = 0;
}
System.out.println();
System.out.println("Enter a phrase you wish to encrypt: ");
while (!in1.hasNextLine())
{
in1.nextLine();
}
String entry= in1.nextLine();
entry = entry.toLowerCase();
System.out.println("\nThis is your encrypted message:\n" + CaesarShiftEncryption.encrypt(alphabet, entry, shift));
}
else
{
Scanner in1 = new Scanner(System.in);
System.out.println("Enter an integer value between 1-25 by which you desire to shift your message: ");
while (!in.hasNextInt())
{
System.out.print("\nPlease enter an integer value: ");
in.nextLine();
}
int shift = in.nextInt();
if(shift > 25 || shift < 1)
{
shift = 0;
}
System.out.println();
System.out.println("Enter a phrase you wish to decrypt: ");
while (!in1.hasNextLine())
{
in.nextLine();
}
String entry= in1.nextLine();
entry = entry.toLowerCase();
System.out.println("\nThis is your decrypted message:\n" + CaesarShiftDecryption.decrypt(alphabet, entry, shift));
}
}
}
任何人都可以解释为什么这个修复了这个程序的第一个版本出现的问题? (我以前经历过这个,但从来不知道为什么 - 虽然我已经这样修复了[在输入中添加了一个额外的扫描仪对象]。)
答案 0 :(得分:0)
更改为此应解决问题
Old - String entry= in.nextLine();
New - String entry= in.next();
答案 1 :(得分:0)
您好,问题在于您的表现不佳 改变这个:
while (!in.hasNextLine())
{
in.nextLine();
}
String entry= in.nextLine();
到:
String entry = "";
while(entry.equals(""))
{
entry = in.nextLine();
}
不要忘记在你的两个功能中改变它;)
我只是尝试了它,它为我工作;)
更新 *
您可以通过添加以下内容来查看问题:
String entry= in.nextLine();
System.out.println("entry//" + entry+"//");
输出=
Enter a phrase you wish to encrypt:
entry////