开发者

Cannot convert double to boolean (noob here)

开发者 https://www.devze.com 2023-02-26 04:29 出处:网络
This is my algorithm, and my goal here is to set an \"if statement\". Take a look: import java.util.Scanner;

This is my algorithm, and my goal here is to set an "if statement". Take a look:

import java.util.Scanner;

public class apple {
    public static void main(String args[]) {
        System.out.println("This is occurring right now");

        for (float ind = 0; ind <= 0.50; ind += 0.1) {
            System.out.println(ind);
        }

        double find;
        if (find = 0.25);

At last line, it says "I cannot convert double to boolean".

How should I proceed? (I'm a begginer with little time to finish this assignment, like today)


Okay, I looked all of your tips and came up with that:

import java.util.Scanner;

public class apple {

    public static void main(String args[]) {
        System.out.println("this is occouring right now");

        for (float ind = 0; ind <= 0.50; ind += 0.1) {
            System.out.println(ind);
        }

        double find = 0;
        if (find == 0.25) {
            System.out.println("Group A has been Warned"开发者_运维技巧);
        } else if (find == 0.30) {
            System.out.println("Group A and B need to shut down");
        } else if (find == 0.40) {
            System.out.println("All Groups (A, B and C) need to shut down");
        }
    }
}

I thought that by setting the if statement there, it would show me the warnings when it runs.

How do I call again what's being typed inside System.out.println()?

I'm always calling print-outs... What now?


tweaked

 import java.util.Scanner;

public class apple {
public static void main(String args []) {
    Scanner dic = new Scanner(System.in);
    System.out.println("type the rate: ");
    for (double ind=dic.nextDouble(); ind <=0.50; ind+=0.1){

        // include my variables

        if (ind == 0.2){
        System.out.println("Group A has been Warned");

    }else if (ind == 0.3){
        System.out.println("Group A and B need to shut down");
    }else if (ind == 0.4){
        System.out.println("All Groups (A, B and C) need to shut down");
    }
}
}
}

i guess im going into the right direction with that one. donno. the thing is, I typed 0.2 and it showed two warnings. It should show only one. I suppose.


if (find == 0.25) - comparison is done via ==, not =, which is for assignment.

The error message means that you are trying to assign a double value to a boolean (which is not possible)

By the way, your code might not work because of the way doubles are represented. If you want to have precise double calculations, either use BigDecimal, or use ints (your doubles multiplied by 10 or 100)


Use = for assignment and == for equality-checks.

Later you'll stumble over the fact that floating point numbers don't work as you expect ...


If statement wants a boolean inside the parentheses

you can check this link

import java.util.Scanner;

public class apple {

public static void main(String args []) {    

System.out.println("this is occurring right now");    

for (float ind=0; ind <=0.50; ind+=0.1){        
System.out.println(ind);    
}    

double find;    

if (find == 0.25);// when it's "=" it sets find as 0.25, when it is "==" it compares the two values

version two: If you want only one if to work you should use a break at the end of the if statements what this will do is that after processing the if statements which meets your ind condition it will get out of the loop.

import java.util.Scanner;
public class apple {
   public static void main(String args []) {

     Scanner dic = new Scanner(System.in);    
     System.out.println("type the rate: "); 

     for (double ind=dic.nextDouble(); ind <=0.50; ind+=0.1){        
     // include my variables        
        if (ind == 0.2){        
          System.out.println("Group A has been Warned");  
          break;  
        }
        else if (ind == 0.3){        
          System.out.println("Group A and B need to shut down");  
          break;  
        }
         else if (ind == 0.4){        
           System.out.println("All Groups (A, B and C) need to shut down"); 
           break;   
         }
      }
   }
}


It's not always good to compare fractional numbers via ==. Numbers are represented in the computer in a different way than we are writing, so 0.2 + 0.1 may be a tiny bit different from 0.3. So instead of

if (ind == 0.2){

you'd better use something like

if (Math.abs(ind - 0.2) < 0.001){


Both Bozho and Joachim answered the question, I will just add that if in java requires boolean expression inside and you give it the double - find. (find = 0.25) is assignment to find, but is not part of the evaluation. That's why you get

it cannot convert double to boolean


In addition to what everyone else said, watch out for the semi-colon at the end of the if-statement.

This piece of code:

if (find == 0.25);
System.out.println("Hello");

Will always print Hello.

However:

if (find == 0.25)
System.out.println("Hello");

This one will only print Hello if the if statement is fulfilled.


if statement uses boolean values or expressions using operators like ==, !=, >, <, >= and <=.

I don't know what are you expecting from your code, but your "find" variable has no value, and you wanted to use "ind", it would go out of scope.

Your code has no way that it will ever have 0.25 anywhere


double find;
    if (find = 0.25);

You're never assigning anyting to find, so the if (if you correct the == problem) will never be true.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号