Java 如何修正方法以输出一个答案?
这就是我的方法,它可以显示两个数是否相等。最后一个if语句是,如果所有的数字都相同,但是当我运行这个语句时,它会打印“两个并列第二个”和“所有并列第一个”。如果所有的数字都相同,它将只输出“所有的第一个都是并列的”,我该怎么做呢Java 如何修正方法以输出一个答案?,java,Java,这就是我的方法,它可以显示两个数是否相等。最后一个if语句是,如果所有的数字都相同,但是当我运行这个语句时,它会打印“两个并列第二个”和“所有并列第一个”。如果所有的数字都相同,它将只输出“所有的第一个都是并列的”,我该怎么做呢 在处理其他条件语句之前,先将该语句放在开头。并确保使用if和if else语句。在处理其他条件语句之前,先将该语句放在开头。并确保使用if和if else语句。在处理其他条件语句之前,先将该语句放在开头。并确保使用if和if else语句。在处理其他条件语句之前,先将该
在处理其他条件语句之前,先将该语句放在开头。并确保使用
if
和if else
语句。在处理其他条件语句之前,先将该语句放在开头。并确保使用if
和if else
语句。在处理其他条件语句之前,先将该语句放在开头。并确保使用if
和if else
语句。在处理其他条件语句之前,先将该语句放在开头。并确保使用if
和if-else
语句。尝试使用else-if
另外,将最后一个条件放在第一位,否则它将在更早的时候变为真:
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
}
else if (a==b) {
System.out.println("Two tied for second");
}
else if (c==b) {
System.out.println("Two tied for second");
}
else if (c==a) {
System.out.println("Two tied for second");
}
如果,请尝试使用
else
另外,将最后一个条件放在第一位,否则它将在更早的时候变为真:
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
}
else if (a==b) {
System.out.println("Two tied for second");
}
else if (c==b) {
System.out.println("Two tied for second");
}
else if (c==a) {
System.out.println("Two tied for second");
}
如果
,请尝试使用else
另外,将最后一个条件放在第一位,否则它将在更早的时候变为真:
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
}
else if (a==b) {
System.out.println("Two tied for second");
}
else if (c==b) {
System.out.println("Two tied for second");
}
else if (c==a) {
System.out.println("Two tied for second");
}
如果
,请尝试使用else
另外,将最后一个条件放在第一位,否则它将在更早的时候变为真:
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
}
else if (a==b) {
System.out.println("Two tied for second");
}
else if (c==b) {
System.out.println("Two tied for second");
}
else if (c==a) {
System.out.println("Two tied for second");
}
正如其他帖子所说,使用elseif也是一个好主意,但要记住的是,当它满足一个elseif的条件时,它不会再继续下去。因此,如果它发现a==b,它将不再进一步检查c==b或c==a
怎么样
public static void overlap(double a, double b, double c) {
if(a==b && b==c && a==c) {
System.out.println("a==b==c");
}
else {
if (a==b) {
System.out.println("a==b");
}
if (c==b) {
System.out.println("c==b");
}
if (c==a) {
System.out.println("c==a");
}
}
}
正如其他帖子所说,使用elseif也是一个好主意,但要记住的是,当它满足一个elseif的条件时,它不会再继续下去。因此,如果它发现a==b,它将不再进一步检查c==b或c==a
怎么样
public static void overlap(double a, double b, double c) {
if(a==b && b==c && a==c) {
System.out.println("a==b==c");
}
else {
if (a==b) {
System.out.println("a==b");
}
if (c==b) {
System.out.println("c==b");
}
if (c==a) {
System.out.println("c==a");
}
}
}
正如其他帖子所说,使用elseif也是一个好主意,但要记住的是,当它满足一个elseif的条件时,它不会再继续下去。因此,如果它发现a==b,它将不再进一步检查c==b或c==a
怎么样
public static void overlap(double a, double b, double c) {
if(a==b && b==c && a==c) {
System.out.println("a==b==c");
}
else {
if (a==b) {
System.out.println("a==b");
}
if (c==b) {
System.out.println("c==b");
}
if (c==a) {
System.out.println("c==a");
}
}
}
正如其他帖子所说,使用elseif也是一个好主意,但要记住的是,当它满足一个elseif的条件时,它不会再继续下去。因此,如果它发现a==b,它将不再进一步检查c==b或c==a
怎么样
public static void overlap(double a, double b, double c) {
if(a==b && b==c && a==c) {
System.out.println("a==b==c");
}
else {
if (a==b) {
System.out.println("a==b");
}
if (c==b) {
System.out.println("c==b");
}
if (c==a) {
System.out.println("c==a");
}
}
}
你有两个问题
第一个是“if”语句的求值顺序。
听起来你想先评估最后一个
第二个问题是,您需要打印匹配的第一个“if”语句的结果,并跳过其余语句
有很多方法可以做到这一点。
一种流行的方法是拥有一系列if/elseif语句,但我发现它的可读性不如我喜欢的。因此,我使用以下有点不标准的方法:
do {
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
break;
}
if (a==b) {
System.out.println("Two tied for second");
break;
}
if (c==b) {
System.out.println("Two tied for second");
break;
}
if (c==a) {
System.out.println("Two tied for second");
break;
}
} while(false);
你有两个问题
第一个是“if”语句的求值顺序。
听起来你想先评估最后一个
第二个问题是,您需要打印匹配的第一个“if”语句的结果,并跳过其余语句
有很多方法可以做到这一点。
一种流行的方法是拥有一系列if/elseif语句,但我发现它的可读性不如我喜欢的。因此,我使用以下有点不标准的方法:
do {
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
break;
}
if (a==b) {
System.out.println("Two tied for second");
break;
}
if (c==b) {
System.out.println("Two tied for second");
break;
}
if (c==a) {
System.out.println("Two tied for second");
break;
}
} while(false);
你有两个问题
第一个是“if”语句的求值顺序。
听起来你想先评估最后一个
第二个问题是,您需要打印匹配的第一个“if”语句的结果,并跳过其余语句
有很多方法可以做到这一点。
一种流行的方法是拥有一系列if/elseif语句,但我发现它的可读性不如我喜欢的。因此,我使用以下有点不标准的方法:
do {
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
break;
}
if (a==b) {
System.out.println("Two tied for second");
break;
}
if (c==b) {
System.out.println("Two tied for second");
break;
}
if (c==a) {
System.out.println("Two tied for second");
break;
}
} while(false);
你有两个问题
第一个是“if”语句的求值顺序。
听起来你想先评估最后一个
第二个问题是,您需要打印匹配的第一个“if”语句的结果,并跳过其余语句
有很多方法可以做到这一点。
一种流行的方法是拥有一系列if/elseif语句,但我发现它的可读性不如我喜欢的。因此,我使用以下有点不标准的方法:
do {
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
break;
}
if (a==b) {
System.out.println("Two tied for second");
break;
}
if (c==b) {
System.out.println("Two tied for second");
break;
}
if (c==a) {
System.out.println("Two tied for second");
break;
}
} while(false);
这会更干净
public static void overlap(double a, double b, double c) {
if (a == b && b == c && a == c) {
System.out.println("All tied for first");
}
else if (a == b || c == b || c == a) {
System.out.println("Two tied for second");
}
}
这会更干净
public static void overlap(double a, double b, double c) {
if (a == b && b == c && a == c) {
System.out.println("All tied for first");
}
else if (a == b || c == b || c == a) {
System.out.println("Two tied for second");
}
}
这会更干净
public static void overlap(double a, double b, double c) {
if (a == b && b == c && a == c) {
System.out.println("All tied for first");
}
else if (a == b || c == b || c == a) {
System.out.println("Two tied for second");
}
}
这会更干净
public static void overlap(double a, double b, double c) {
if (a == b && b == c && a == c) {
System.out.println("All tied for first");
}
else if (a == b || c == b || c == a) {
System.out.println("Two tied for second");
}
}
代码的问题在于它独立地计算每个if
语句。因此,如果未能指定else
语句,则无论语句的顺序如何,如果将每个if
语句计算为TRUE
,则将执行该语句
按照与Moishe相同的思路(也许出于您的目的更直观),您还可以按如下方式构造代码:
public static void overlap(double a, double b, double c){
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
}
}
else {
if (a==b) {
System.out.println("Two tied for second");
}
if (c==b) {
System.out.println("Two tied for second");
}
if (c==a) {
System.out.println("Two tied for second");
}
}
}
然而,如果你想简化一些事情,你可以考虑重构事物以防止过度的比较:
public static void overlap(double a, double b, double c){
if (a==b) {
if(b==c && a==c) {
System.out.println("All tied for first");
}
else {
System.out.println("Two tied for second");
}
}
else if (c==b) {
System.out.println("Two tied for second");
}
else if (c==a) {
System.out.println("Two tied for second");
}
}
代码的问题在于它独立地计算每个if
语句。因此,如果未能指定else
语句,则无论语句的顺序如何,如果将每个if
语句计算为TRUE
,则将执行该语句
按照与Moishe相同的思路(也许出于您的目的更直观),您还可以按如下方式构造代码:
public static void overlap(double a, double b, double c){
if(a==b && b==c && a==c) {
System.out.println("All tied for first");
}
}
else {
if (a==b) {
System.out.println("Two tied for second");
}
if (c==b) {
System.out.println("Two tied for second");
}
if (c==a) {
System.out.println("Two tied for second");
}
}
}
然而,如果你想简化一些事情,你可以考虑重构事物以防止过度的比较:
public static void overlap(double a, double b, double c){
if (a==b) {
if(b==c && a==c) {
System.out.println("All tied for first");
}
else {
System.out.println("Two tied for second");
}
}
else if (c==b) {
System.out.println("Two tied for second");
}
else if (c==a) {
System.out.println("Two tied for second");
}
}
代码的问题在于它独立地计算每个if
语句。因此,如果您未能指定else
语句,则每个if
语句将在计算为TRUE
时执行,