英文:
Does anyone know why my else if statements are not working?
问题
以下是翻译好的代码部分:
import java.util.*;
public class sortnumber {
public static void main (String[] args) {
Scanner scan = new Scanner(System.in);
int num1, num2, num3, random;
num1 = scan.nextInt();
num2 = scan.nextInt();
num3 = scan.nextInt();
random = 0;
if (num1 < num2 && num1 < num3)
System.out.print(num1);
if (num2 < num3)
System.out.print(num2);
System.out.print(num3);
else
System.out.print(num3);
System.out.print(num2);
else if (num2 < num1 && num2 < num3)
System.out.print(num2);
if (num1 < num3)
System.out.print(num1);
System.out.print(num3);
else
System.out.print(num3);
System.out.print(num1);
else if (num3 < num2 && num3 < num1)
System.out.print(num3);
if (num2 < num1)
System.out.print(num2);
System.out.print(num1);
else
System.out.print(num1);
System.out.print(num2);
}
}
英文:
This code sorts three numbers from smallest to greatest.
However, my else statements gave me 4 errors, on lines 15, 18, 23, 26, and 31. Does anyone know why this is the case?
import java.util.*;
public class sortnumber {
public static void main (String[] args) {
Scanner scan = new Scanner (System.in);
int num1, num2, num3, random;
num1 = scan.nextInt();
num2 = scan.nextInt();
num3 = scan.nextInt();
random = 0;
if (num1 < num2 && num1 < num3)
System.out.print (num1);
if (num2 < num3)
System.out.print (num2);
System.out.print (num3);
else
System.out.print (num3);
System.out.print (num2);
else if (num2 < num1 && num2 < num3)
System.out.print (num2);
if (num1 < num3)
System.out.print (num1);
System.out.print (num3);
else
System.out.print (num3);
System.out.print (num1);
else if (num3 < num2 && num3 < num1)
System.out.print (num3);
if (num2 < num1)
System.out.print (num2);
System.out.print (num1);
else
System.out.print (num1);
System.out.print (num2);
}
}
答案1
得分: 3
由于你没有使用花括号,你的缩进是错误的。实际上你编写的代码是这样的:
random = 0;
if (num1 < num2 && num1 < num3)
System.out.print(num1);
if (num2 < num3)
System.out.print(num2);
System.out.print(num3);
else // <===== 错误
System.out.print(num3);
System.out.print(num2);
else if (num2 < num1 && num2 < num3) // <===== 错误
System.out.print(num2);
if (num1 < num3)
System.out.print(num1);
System.out.print(num3);
else // <===== 错误
System.out.print(num3);
System.out.print(num1);
else if (num3 < num2 && num3 < num1) // <===== 错误
System.out.print(num3);
if (num2 < num1)
System.out.print(num2);
System.out.print(num1);
else // <===== 错误
System.out.print(num1);
System.out.print(num2);
如果你按照缩进指示添加花括号,代码应该是这样的:
random = 0;
if (num1 < num2 && num1 < num3) {
System.out.print(num1);
if (num2 < num3) {
System.out.print(num2);
System.out.print(num3);
} else {
System.out.print(num3);
System.out.print(num2);
}
} else if (num2 < num1 && num2 < num3) {
System.out.print(num2);
if (num1 < num3) {
System.out.print(num1);
System.out.print(num3);
} else {
System.out.print(num3);
System.out.print(num1);
}
} else if (num3 < num2 && num3 < num1) {
System.out.print(num3);
if (num2 < num1) {
System.out.print(num2);
System.out.print(num1);
} else {
System.out.print(num1);
System.out.print(num2);
}
}
为了避免类似错误,通常建议始终使用花括号。
英文:
Since you don't have curly braces, your indentations are wrong. What you actually coded is this:
random = 0;
if (num1 < num2 && num1 < num3)
System.out.print (num1);
if (num2 < num3)
System.out.print (num2);
System.out.print (num3);
else // <===== ERROR
System.out.print (num3);
System.out.print (num2);
else if (num2 < num1 && num2 < num3) // <===== ERROR
System.out.print (num2);
if (num1 < num3)
System.out.print (num1);
System.out.print (num3);
else // <===== ERROR
System.out.print (num3);
System.out.print (num1);
else if (num3 < num2 && num3 < num1) // <===== ERROR
System.out.print (num3);
if (num2 < num1)
System.out.print (num2);
System.out.print (num1);
else // <===== ERROR
System.out.print (num1);
System.out.print (num2);
If you add the braces indicated by your indentations, you get this:
random = 0;
if (num1 < num2 && num1 < num3) {
System.out.print(num1);
if (num2 < num3) {
System.out.print(num2);
System.out.print(num3);
} else {
System.out.print(num3);
System.out.print(num2);
}
} else if (num2 < num1 && num2 < num3) {
System.out.print(num2);
if (num1 < num3) {
System.out.print(num1);
System.out.print(num3);
} else {
System.out.print(num3);
System.out.print(num1);
}
} else if (num3 < num2 && num3 < num1) {
System.out.print(num3);
if (num2 < num1) {
System.out.print(num2);
System.out.print(num1);
} else {
System.out.print(num1);
System.out.print(num2);
}
}
In order to prevent mistakes like that, it is commonly recommended to always use curly braces.
答案2
得分: 1
一个if/else if/else语句在其下的代码块超过一行时需要使用花括号({,})。例如,你的第一个if语句是完全有效的。然而,在其余的代码中存在错误。我建议几乎总是使用花括号,即使在if语句下只有一行代码,因为这通常可以使你的代码更易读。
英文:
An if/else if/else statement requires curly braces ({, }) if the block of code under it is more than one line. For example, your first if statement is completely valid. However, errors lie within the rest of the code. I would suggest using curly braces almost always though, as it typically makes your code more readable (even if there is only one line under the if statement).
答案3
得分: 0
你缺少花括号。
Java代码不关心新行从哪里开始以及缩进有多少。取而代之的是使用行尾字符(分号)和花括号。
尽管Java支持无花括号的if/else语句,但它们只将一个语句与if
或else
关联起来。最佳实践是始终在条件语句中包含要包含在其中的代码的花括号。
例如,看一下你的第一个嵌套if
语句:
if (num1 < num2 && num1 < num3) {
System.out.print (num1);
if (num2 < num3) {
System.out.print (num2);
System.out.print (num3);
}
else {
System.out.print (num3);
System.out.print (num2);
}
}
请查看这个页面,了解一些旧但有用的指导:http://www.fredosaurus.com/notes-java/flow/if/30if-braces.html
英文:
You are missing curly braces.
Java code does not pay attention to where new lines start or how much indentation there is. Instead, end-of-line characters (semi-colons) and curly braces are used.
While braceless if/else statements are supported in Java, they only associate one statement with the if
or the else
. It is considered best practice to always include the curly braces around the code you wish to include in your conditional statement.
For example, taking your first nested if
statement:
if (num1 < num2 && num1 < num3) {
System.out.print (num1);
if (num2 < num3) {
System.out.print (num2);
System.out.print (num3);
}
else {
System.out.print (num3);
System.out.print (num2);
}
}
Take a look at this page for some old but useful pointers: http://www.fredosaurus.com/notes-java/flow/if/30if-braces.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论