Java 为什么我的程序不接受我返回的布尔结果?

Java 为什么我的程序不接受我返回的布尔结果?,java,Java,如果x和y都是“true”,我只想返回true 我想使用switch/case方法,而不是简单的if/else 但是我无法获得接受返回结果的方法 你们有什么想法吗 public class test2 { public static boolean f(boolean x, boolean y){ switch(x + "-" + y){ case "true-false": return false

如果x和y都是“true”,我只想返回true

我想使用switch/case方法,而不是简单的if/else

但是我无法获得接受返回结果的方法

你们有什么想法吗

 public class test2 {
     public static boolean f(boolean x, boolean y){
        switch(x + "-" + y){            
        case "true-false": 
             return false; //i also tried "return x" or "return y"
        case "false-true": 
             return false;
        case "true-true": 
             return true;
        case "false-false": 
             return false; 
        }
    }


    public static void main(String[]args){
        f(false,true);
    }   
}

首先,你的代码不会编译,而且它不是一个有效的算法

一个有效的算法应该类似于
返回x&&y
,而不是使用字符串中的开关

无需将
布尔值
转换为
字符串
。有很多种方法可以实现你想要做的事情

public static boolean f(boolean x, boolean y){
    return x && y;
}
通过方法修复编译错误#1

默认值
大小写添加到
开关
,如下所示

public static boolean f(boolean x, boolean y){

     switch(x + "-" + y){

        case "true-false": 
             return false; //i also tried "return x" or "return y"
        case "false-true": 
             return false;
        case "true-true": 
             return true;
        case "false-false": 
             return false; 
        default:
            return false;
      }

}
通过方法2修复编译错误


在案例结束语句中添加默认值

default:
   return true;
public class Test {
   public static boolean f(boolean x, boolean y){
        switch(x + "-" + y){
            case "true-false":
                return false; //i also tried "return x" or "return y"
            case "false-true":
                return false;
            case "true-true":
                return true;
            case "false-false":
                return false;
            default: return false;
        }
    }

    public static void main(String[]args){
        System.out.println(f(false,true));
    }
}
然后要在控制台中查看函数返回的结果,请使用
System.out.println()
在控制台中查看

System.out.println(f(false,true));

首先,程序不会按原样编译。编译器将抱怨方法
f
缺少返回语句。
开关
是详尽的,但是编译器无法推断这种详尽性,因此,如果没有匹配的
大小写
,编译器会抱怨缺少返回。一种可能的解决方案是添加一个
默认值
案例。另一种可能是在方法末尾添加
返回值
。在我看来,这两种解决方案都是次优的,稍后将详细介绍

在给定的修复程序中,代码按预期工作。结果不会显示在屏幕上,因为它从未打印过。该程序的一个稍加修改的版本给出了预期的结果:

class Ideone {
  public static boolean f(final boolean x, final boolean y) {
    switch (x + "-" + y) {
      case "true-false":
        return false;
      case "false-true":
        return false;
      case "true-true":
        return true;
      case "false-false":
        return false;
    }
    return false;
  }

  public static void main(final String[] args) {
    System.out.println(f(false, false));
    System.out.println(f(false, true));
    System.out.println(f(true, false));
    System.out.println(f(true, true));
  }
}



我建议另一种解决方案:与其构造两个参数的
字符串
-表示,不如返回两个参数的AND乘积:
x&&y
。首先,这消除了次优的
返回
(尽管是在方法末尾或
默认情况下)。另一方面,这更有效,wrt。计算时间和内存消耗。

您的代码太复杂,可以简单地更改为:

 public class test2 {
 public static boolean f(boolean x, boolean y){
    return x && y;
}


public static void main(String[]args){
    f(false,true);
}   

}应该有一个默认的return语句,它不会编译,因为它请求return语句

default:
   return true;
public class Test {
   public static boolean f(boolean x, boolean y){
        switch(x + "-" + y){
            case "true-false":
                return false; //i also tried "return x" or "return y"
            case "false-true":
                return false;
            case "true-true":
                return true;
            case "false-false":
                return false;
            default: return false;
        }
    }

    public static void main(String[]args){
        System.out.println(f(false,true));
    }
}

为什么不返回
x&&y
?这里解释了失败的原因:(但我同意图灵85,你不应该这样做)。@AlexB这不是真的。如果
开关中使用了
字符串
s,则不知道(或预期)此行为。谢谢(但现在我想知道为什么原来的代码不起作用,现在就去测试:)@Turing85哦,是的。这就容易多了。这些案例是详尽无遗的。因此,
默认值
-情况是多余的。这并不能解决核心问题。谢谢!现在我知道返回x&&y要简单得多,但是如果我想使用switch/case,我需要做的就是添加“default”。你们rock@Turing85由于此场景中缺少默认情况,导致出现错误。我同意有更好的编码方法。我试着向他解释为什么switch语句出错。@Turing85不是吗?现在我的程序运行。如果x和y为真,那么我的程序返回真。当然,x&&y要好得多。问题中的代码按预期工作,没有默认值,请参阅。唯一缺少的是一个return语句(或default)语句来编译它。换句话说:问题是编译问题,而不是逻辑问题。是的,我完全理解这是太多的代码。我只是好奇我做错了什么。谢谢你对我的小问题给出了详细的答案。@java看起来你接受了错误的答案