如果在Java中使用If Else语句。

huangapple go评论73阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年9月22日 06:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64000808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定