Java 正确的代码不会编译吗?

Java 正确的代码不会编译吗?,java,eclipse,compiler-errors,Java,Eclipse,Compiler Errors,这段代码对我来说似乎很好(CS101),但我的IDE抛出了一个错误“此方法必须返回布尔类型的结果” 我不想要任何关于如何简化我的代码或类似的东西的提示,只是想要一个原因/解决方案来解释为什么会发生这种情况 public static boolean validation(String correct1, String correct2) { Scanner in = new Scanner(System.in); boolean correctInput = false;

这段代码对我来说似乎很好(CS101),但我的IDE抛出了一个错误“此方法必须返回布尔类型的结果”

我不想要任何关于如何简化我的代码或类似的东西的提示,只是想要一个原因/解决方案来解释为什么会发生这种情况

public static boolean validation(String correct1, String correct2)
{
   Scanner in = new Scanner(System.in);

    boolean correctInput = false;
    String userInput;

    while (correctInput == false)
    {
        System.out.print("Type in " + correct1 + " or " + correct2);
        userInput = in.next();

        if ( userInput.equals(correct1) )
        {
            return true;
        }else if ( userInput.equals(correct2) )
        {
            return false;
        }else
        {
            System.out.println("Try again!");
        }
    }   
}
问题现在解决了,有兴趣的人为什么我需要下面的完整代码:

import java.util.*;
public class CheckingInput
{
public static void main(String args[])
{
    System.out.println("What is 1+1?");
    boolean answer = validation("two", "three");
    if(answer == true)
    {
        System.out.print("Correct!");
    }else if(answer == false)
    {
        System.out.print("Wrong!");
    }
}

public static boolean validation(String correct1, String correct2)
{
    Scanner in = new Scanner(System.in);
    boolean correctInput = false;
    String userInput;

    while (correctInput == false)
    {
        System.out.print("Type in " + correct1 + " or " + correct2 + ": ");
        userInput = in.next();

        if ( userInput.equals(correct1) )
        {
            correctInput = true;
            return true;

        }else if (userInput.equals(correct2))
        {
            correctInput = true;
            return false;
        }else
        {
            System.out.println("Try again!");
            correctInput = false;
        }
    }   
    return false;// Doesn't really matter, loop will never reach here
}

A
返回true
返回false
语句在函数结束前丢失,以防循环退出。

我假设无限循环是有意的,因为您正在等待用户的响应,所以请尝试以下操作:

public static boolean validation(String correct1, String correct2)
{
    Scanner in = new Scanner(System.in);
    String userInput;

    while (true)
    {
        System.out.print("Type in " + correct1 + " or " + correct2);
        userInput = in.next();

        if ( userInput.equals(correct1) )
        {
            return true;
        }
        else if ( userInput.equals(correct2) )
        {
            return false;
        }
        else
        {
            System.out.println("Try again!");
        }
    }

    return false; // Doesn't really matter, loop will never reach here
}

那就不对了。您的代码是否在所有情况下都返回布尔值?另一方面,为什么要使用
=
而不是
.equals(
对于字符串?我希望它做的是重复while循环,直到两个if语句中的一个得到满足并返回一个值为止,并且在它们得到满足之前不要退出,我是不是走错了路?@Chris:行得通,请看我的答案。不过,我不明白在实践中为什么要这样做;应该给用户一种方法,让用户在没有满足两个
if
语句中的一个。@SotiriosDelimanolis这怎么可能是比较字符串的重复?这与此无关,我不希望循环退出,直到满足两个if语句中的一个并返回一个值。这已经起作用了,我知道这在实践中不会出现,但我的讲师正试图让你知道让我们在不知道他的情况下练习验证技术,愚蠢的问题,愚蠢的代码