Java 导致无法打印给定输出的逻辑错误

Java 导致无法打印给定输出的逻辑错误,java,object,input,logic,java.util.scanner,Java,Object,Input,Logic,Java.util.scanner,运行此程序时,它不会打印加密或解密输出,并打印用户输入消息的提示,但实际上不允许输入 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"

运行此程序时,它不会打印加密或解密输出,并打印用户输入消息的提示,但实际上不允许输入

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;
    }
}
但是,我构造了一个额外的scanner对象,我的测试仪代码如下所示:

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));
        }
    }
}

有人能解释一下为什么这个程序的第一个版本会解决这个问题吗?我以前也经历过这种情况,但不知道为什么,尽管我已经用这种方式修复了它[向输入中添加一个额外的扫描仪对象]。

更改为这种方式应该可以解决问题

Old - String entry= in.nextLine();

New - String entry= in.next();

你好,你的问题是你的while做得不好 更改此项:

       while (!in.hasNextLine())
        {
            in.nextLine();
        }
        String entry= in.nextLine();
致:

别忘了在你的两个功能中都改变它

我刚试过,它对我有效

更新*

您可以通过添加以下内容查看您的问题:

        String entry= in.nextLine();
        System.out.println("entry//" + entry+"//");
输出=

输入要加密的短语:
entry//

不,它不会,下一个方法提供完全不同的功能。虽然这可能是一个修复,但它没有解决我的问题。你的问题是什么?为什么会中断?显然,我的问题并不像我想象的那样具体,我相信我是在一个txt文件中起草的,但没有发布我编辑过的版本。重读我文章的最后一部分。再一次,对不起。经过一些测试,它很简单,永远不会通过!在.HasNeXLead中,它直接进入它被认为是或null,所有这一切都在ToLoeCaseand到您的功能和崩溃:
        String entry= in.nextLine();
        System.out.println("entry//" + entry+"//");