Java 自动跳过输入

Java 自动跳过输入,java,Java,它跳过了这个userInput,在我输入文本之前,它会检查它并要求输入shift键。更改: System.out.println("Please enter your text that you want to encrypt: "); String userInput = in.nextLine(); System.out.print("Please enter your shift key(0-25): "); 到 这修复了

它跳过了这个
userInput
,在我输入文本之前,它会检查它并要求输入shift键。

更改:

System.out.println("Please enter your text that you want to encrypt: ");
                String userInput = in.nextLine();

                System.out.print("Please enter your shift key(0-25): ");

这修复了它(在Eclipse中测试):

Scanner-in=新的扫描仪(System.in);
System.out.print(“请选择。('e'加密,'d'解密,'q'退出):”;
字符串userInput=in.nextLine();
if(userInput.equals(“e”))
{
System.out.println(“请输入您要加密的文本:”;
userInput=in.nextLine();
系统输出打印(“请输入您的班次键(0-25):”;
int userS=Integer.parseInt(in.nextLine());
如果(用户<0 | |用户>25)
{
System.out.print(“无效的移位键,请输入有效的移位键:”);
userS=Integer.parseInt(in.nextLine());
}
}
in.close();
我将您的
userIn
变量更改为
userInput
,因为我们不需要它;您的
next()
调用也更改为
nextLine()

我还将所有的
nextLine()
更改为
nextLine()
。这将帮助您避免以后出现
异常


最后,在使用完
扫描仪后,务必关闭它,以节省系统资源。

简单,将代码更改为

Scanner in = new Scanner(System.in);
System.out.print("Please choose.('e' to encrypt, 'd' to decrypt, 'q' to quit): ");
String userInput = in.nextLine();
if (userInput.equals("e"))
{
    System.out.println("Please enter your text that you want to encrypt: ");
    userInput = in.nextLine();

    System.out.print("Please enter your shift key(0-25): ");
    int userS = Integer.parseInt(in.nextLine());
    if (userS < 0 || userS > 25)
    {
        System.out.print("Invalid shift key, please enter a valid shift key: ");
        userS = Integer.parseInt(in.nextLine());
    }
}
in.close();

如果你这样做,你就不能输入一个句子。您只能输入一个单词。
String userInput = in.nextLine()
in.nextLine(); String userInput = in.nextLine();
Scanner in = new Scanner(System.in);
System.out.print("Please choose.('e' to encrypt, 'd' to decrypt, 'q' to quit): ");
String userInput = in.nextLine();
if (userInput.equals("e"))
{
    System.out.println("Please enter your text that you want to encrypt: ");
    userInput = in.nextLine();

    System.out.print("Please enter your shift key(0-25): ");
    int userS = Integer.parseInt(in.nextLine());
    if (userS < 0 || userS > 25)
    {
        System.out.print("Invalid shift key, please enter a valid shift key: ");
        userS = Integer.parseInt(in.nextLine());
    }
}
in.close();
String userInput = in.next();