如果不是Java

陈敏敏:

我正在为我的AP CompSci类做一个小项目,基本上,它只是接受输入,确定它是否是积极的,我对此深信不疑

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");
       }
       
       }

它无法运行,“ PositiveNumber.java:18:错误:解析时已到达文件末尾”

我在使用If Else吗?我们尚未了解If And Else,该项目仅需要布尔值,但我希望它更具体。谢谢您的帮助。

Aniox:

正确的语法是:

    if(condition){
        //Deal with condition
    }else{
        //Deal with all other conditions
    }

因此,if-else应该如下所示:

   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");
   }
   

这意味着您的代码应如下所示:

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");
       } 
       
 }
      
}
     

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章