尽管条件失败,循环仍继续运行

时间:2019-11-19 05:33:16

标签: java

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String sent = "y";
    int a = 0;
    int b = 0;
    System.out.println("Welcome!\nThis program compares two letters in a sentence.");
    while(!sent.equalsIgnoreCase("X")) {
        System.out.print("Enter a sentence or X to exit: ");
        sent = input.next();

        System.out.print("Enter the numeric location of the first letter: ");
        while(!input.hasNextInt()) { 
            System.out.println("Error! Please enter a number not text!");
            input.nextLine(); 

            System.out.print("Enter the numeric location of the first letter: "); 
        }
        a = input.nextInt(); 


        System.out.print("Enter the numeric location of the second letter: ");
        while(!input.hasNextInt()) { 
            System.out.println("Error! Please enter a number not text!");
            input.nextLine(); 

            System.out.print("Enter the numeric location of the second letter: "); 
        }
        b = input.nextInt(); 

       System.out.println();

       char c = sent.charAt(a);
       char d = sent.charAt(b);

       if(c==d) {
           System.out.println(c+" and "+d+" are identical.\n");
       }
       else {
           System.out.println(c+ " and "+ d + " are unique characters. \n");
       }    

    }
    System.out.print("GoodBye!");


}

即使您输入X也不知道为什么,此循环仍会继续运行。另外,当您输入文本而不是数字时,错误消息将如下所示: 错误!请输入数字而不是文字! 输入第一个字母的数字位置:错误!请输入数字而不是文字! 输入第一个字母的数字位置:

只有第三句话会接受用户输入。

2 个答案:

答案 0 :(得分:1)

此代码sent = input.next() 没有读完整的句子。它只读一个字。 请尝试使用nextLine()。

此外,您应该在设置sent之后立即测试退出条件。

答案 1 :(得分:-1)

    public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String sent = "y";
    int a = 0;
    int b = 0;
    System.out.println("Welcome!\nThis program compares two letters in a sentence.");
    System.out.print("Enter a sentence or X to exit: ");
    sent = input.next();
    while(!sent.equalsIgnoreCase("X")) {
        //moved accepting input above while condition.This will fix
        //X not recognized issue

        System.out.print("Enter the numeric location of the first letter: ");
        while(!input.hasNextInt()) { 
            System.out.println("Error! Please enter a number not text!");
            input.nextLine(); 

            System.out.print("Enter the numeric location of the first letter: "); 
        }
        a = input.nextInt(); 


        System.out.print("Enter the numeric location of the second letter: ");
        while(!input.hasNextInt()) { 
            System.out.println("Error! Please enter a number not text!");
            input.nextLine(); 

            System.out.print("Enter the numeric location of the second letter: "); 
        }
        b = input.nextInt(); 

       System.out.println();

       char c = sent.charAt(a);
       char d = sent.charAt(b);

       if(c==d) {
           System.out.println(c+" and "+d+" are identical.\n");
       }
       else {
           System.out.println(c+ " and "+ d + " are unique characters. \n");
       }    

    }
    System.out.print("GoodBye!");


 }