while循环导致print语句打印两次

use*_*336 0 java loops while-loop

我的程序加密和解密Caesar Shift代码,但我现在遇到的主要问题是我的用户选择无法正常工作.

import java.util.Scanner;
public class CaesarShiftTester
{
public static void main(String [] args)
{
    Scanner in = new Scanner(System.in);
    String message = "";
    String userChoice = "";
    int shift = 0;

    System.out.println("1 - Encrypt\n2 - Decrypt\nQ - Quit Program");
    while(!userChoice.equalsIgnoreCase("Q"))
    {
        System.out.println("Make a selection: ");
        userChoice = in.nextLine();
        if(userChoice.equalsIgnoreCase("1"))
        {
            System.out.print("Please enter a message to be encrypted: ");
            message = in.nextLine();

            System.out.print("Please enter a shift value for the cipher(0 - 25): ");
            shift = in.nextInt();

            CaesarShiftEncryption.genCipherAlphabet(shift);
            System.out.println(CaesarShiftEncryption.encrypt(message));
        }

        else if(userChoice.equalsIgnoreCase("2"))
        {
            System.out.print("Please enter a message to be decrypted: ");
            message = in.nextLine();

            System.out.print("Please enter a shift value for the cipher(0 - 25): ");
            shift = in.nextInt();
        }

        else if(userChoice.equalsIgnoreCase("Q"))
        {
            System.out.println("Thanks for using the program!");
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

第一次完成程序后,"进行选择"将被打印两次.这是该类中的另一个文件,该问题在该问题中没有发挥重要作用,但如果您想自己测试这些文件,那么就在这里.请注意我还没有实现解密,所以现在只有"1"和"Q"选项实际上做了什么.

public class CaesarShiftEncryption
{
private static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
private static String newAlphabet = "";

public CaesarShiftEncryption()
{

}

public static String genCipherAlphabet(int shift)
{
    for(int i = 0; i < ALPHABET.length(); i++)
    {
        int alphabetShift = i + shift;
        alphabetShift = alphabetShift % 26;
        newAlphabet += ALPHABET.charAt(alphabetShift);
    }
    return newAlphabet;
}

public static String encrypt(String message)
{
    int letter = 0;
    String encoded = "";
    char[] messageLetters = new char[message.length()];

    for(int i = 0; i < message.length(); i++)
    {
        messageLetters[i] = message.charAt(i);
    }

    for(int a = 0; a < message.length(); a++)
    {
        if(Character.isWhitespace(messageLetters[a]))
        {
            encoded += " ";
            a++;
        }

        if(Character.isUpperCase(messageLetters[a]))
            messageLetters[a] = Character.toLowerCase(messageLetters[a]);

        letter = ALPHABET.indexOf(messageLetters[a]);  
        encoded += newAlphabet.substring(letter, letter + 1).toUpperCase();
    }
    return encoded;
}
}
Run Code Online (Sandbox Code Playgroud)

Hen*_*nry 5

当您使用时读取移位值

shift = in.nextInt();
Run Code Online (Sandbox Code Playgroud)

线尾保留在扫描仪中.在下一个循环迭代中,读取该剩余的行结束,但由于没有找到有效的输入(1,2或Q),循环将再次运行.

要解决这个问题,请按以下方式读取您的班次值:

shift = in.nextInt();
in.nextLine();
Run Code Online (Sandbox Code Playgroud)