Java 使用 if else 语句终止程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19266947/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Termination of program using if else statement?
提问by Quinn
trying to terminate program using negative numbers and if else statement . does anyone see whats wrong with this thanks.
尝试使用负数和 if else 语句终止程序。有没有人看到这有什么问题,谢谢。
import java.util.Scanner;
public class Assignment {
public static void main(String args[]){
int n;
int i=0;
System.out.print("Enter a Number:");
Scanner scanner = new Scanner(System.in);
n= scanner.nextInt();
int backUp = n;
if(n>0)
n=n/10;
i++;
else if(backUp = -1)
System.out.print("program terminated......");
System.exit(0);
System.out.println("Number of Digits in " +backUp +" is " +i);
}
}
回答by BobTheBuilder
First of all, =
is for assigning values. Use ==
for comparing.
首先,=
用于赋值。使用==
比较。
Also, you need to use {}
after if
and else
statements if you want to run more than one line.
此外,如果要运行多于一行,则需要使用{}
afterif
和else
语句。
回答by Suresh Atta
else if(backUp = -1)
Should be
应该
else if(backUp == -1)
= assignment operator , ==
is for comparing
= 赋值运算符, ==
用于比较
And finally with missed {}
最后与错过 {}
if (n > 0) {
n = n / 10;
i++;
} else if (backUp == -1) {
System.out.print("program terminated......");
System.exit(0);
}else{
// do something else. I have no idea.
}
回答by Tobb
You are missing { }
for your if
-statements. In if
statements without the { }
, only the line following the if-statement will be affected by the outcome of the if-test
.
这是因为丢失{ }
了你的if
-statements。在if
没有 的语句中{ }
,只有 if 语句后面的行会受到 的结果的影响if-test
。
So:
所以:
if (condition)
doSomething();
doSomethingElse();
will execute doSomething()
if condition == true
and doSomethingElse()
no matter if condition == true
.
将执行doSomething()
ifcondition == true
并且doSomethingElse()
不管 if condition == true
。
if (condition) {
doSomething();
doSomethingElse();
}
will execute both doSomething()
and doSomethingElse()
, if and only if condition == true.
将同时执行doSomething()
and doSomethingElse()
,当且仅当 condition == true 。
回答by Scott Pearson
You are using an assignment operator to evaluate a condition.
您正在使用赋值运算符来评估条件。
else if(backUp = -1)
should be
应该
else if(backup == -1)
回答by Rajendra arora
remove else
use if(backup==-1).
删除else
使用if(backup==-1).
回答by Alowaniak
First of all your indenting.
首先你的缩进。
Secondly, if you want to execute multiple statements given a certain condition you'll need to put it in a code block like if(x) { /* do multiple things */ }
.
其次,如果你想在给定的条件下执行多条语句,你需要把它放在像if(x) { /* do multiple things */ }
.
Thirdly, your else if(backUp = -1)
is invalid because you need a boolean expression inside a if, backUp = -1
is an assignment and thus does not evaluate to a boolean (you probably want backUp == -1).
第三,您else if(backUp = -1)
的无效,因为您需要在 if 中使用布尔表达式,这backUp = -1
是一个赋值,因此不会计算为布尔值(您可能想要 backUp == -1)。
And you probably want to loop the n = n/10; i++;
part because now it will never count more than 1 digit.
而且您可能想要循环该n = n/10; i++;
部分,因为现在它的计数永远不会超过 1 位数。