英文:
If Else in Java
问题
import java.util.Scanner;
public class PositiveNumber {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double value = scan.nextDouble();
if (value != 0) {
boolean determine = value > 0;
System.out.println("判断结果:" + determine + ",你输入的数是正数");
} else {
System.out.println("零是一个中性数");
}
}
}
英文:
I'm doing a small project for my AP CompSci class, basically, it's just take the input, determine if it's positive or not and I'm stuck with this
import java.util.Scanner;
public class PositiveNumber
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
double value = scan.nextDouble();
if (value != 0)
{
boolean determine = value > 0;
System.out.println("It is "+ determine + " that the number you entered is positive");
{
System.out.println("Zero is a neutral number");
}
}
It cannot be run, "PositiveNumber.java:18: error: reached end of file while parsing"
Am I using If Else right? We haven't learned about If And Else yet, this project only require boolean but I want it to be more specific. Thank you for your help.
答案1
得分: 1
以下是翻译好的内容:
正确的语法是:
if (条件){
// 处理条件
} else {
// 处理其他所有条件
}
因此你的 if-else 应该类似于:
if (value != 0)
{
boolean determine = value > 0;
System.out.println("你输入的数是正数是" + determine);
}
else {
System.out.println("零是中性数");
}
这意味着你的代码应该如下:
import java.util.Scanner;
public class PositiveNumber
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double value = scan.nextDouble();
if (value != 0) {
boolean determine = value > 0;
System.out.println("你输入的数是正数是" + determine);
} else {
System.out.println("零是中性数");
}
}
}
英文:
The correct syntax is:
if(condition){
//Deal with condition
}else{
//Deal with all other conditions
}
So your if-else should look like:
if (value != 0)
{
boolean determine = value > 0;
System.out.println("It is "+ determine + " that the number you entered is positive");
}else{
System.out.println("Zero is a neutral number");
}
Which means your code should look like:
import java.util.Scanner;
public class PositiveNumber
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
double value = scan.nextDouble();
if (value != 0){
boolean determine = value > 0;
System.out.println("It is "+ determine + " that the number you entered is positive");
}else{
System.out.println("Zero is a neutral number");
}
}
}
答案2
得分: 1
代码如下:
看起来你的语法有点问题。编译器感到困惑。尝试使用以下代码:
import java.util.Scanner;
public class PositiveNumber
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
double value = scan.nextDouble();
if (value != 0)
{
boolean determine = value > 0;
System.out.println("判断结果是" + determine + ",您输入的数是正数");
} else {
System.out.println("零是中性数");
}
}
}
在这里,你完成了PositiveNumber
类的编写,并且在if
语句中添加了else
分支。
英文:
It looks like your syntax is a bit off. The compiler is being confused. Try using:
import java.util.Scanner;
public class PositiveNumber
{
public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
double value = scan.nextDouble();
if (value != 0)
{
boolean determine = value > 0;
System.out.println("It is "+ determine + " that the number you entered is positive");
} else {
System.out.println("Zero is a neutral number");
}
}
}
Here you finish off the PositiveNumber class and also add "else" to the if statement.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论