gpt4 book ai didi

java - 无法将 double 值转换为 boolean 值(这里是菜鸟)

转载 作者:行者123 更新时间:2023-12-02 08:26:33 25 4
gpt4 key购买 nike

这是我的算法,我的目标是设置一个“if 语句”。看看:

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

最后一行显示“我无法将 double 值转换为 boolean 值”。
我应该如何进行? (我是一个初学者,几乎没有时间完成这项作业,就像今天一样)

<小时/>

好吧,我查看了您的所有提示并得出了以下结论:

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

我认为通过在那里设置 if 语句,它会在运行时向我显示警告。

如何再次调用 System.out.println() 中输入的内容?
我总是要求打印输出...现在怎么办?

<小时/>

调整

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

我想我正在朝着正确的方向前进。不知道。问题是,我输入了 0.2,它显示了两个警告。它应该只显示一个。我想。

最佳答案

if (find == 0.25) - 比较是通过 == 完成的,而不是用于赋值的 =

错误消息意味着您正在尝试将 double 值分配给 boolean 值(这是不可能的)

顺便说一句,由于 double 的表示方式,您的代码可能无法工作。如果您想要精确的 double 计算,请使用 BigDecimal 或使用 int ( double 乘以 10 或 100)

关于java - 无法将 double 值转换为 boolean 值(这里是菜鸟),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5703233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com