简体   繁体   English

为什么即使我声明了 rand,它也会给我一个错误?

[英]Why does it give me an error even though I declared rand?

import java.util.Random;

class perro{
    private Float animo;
    private Float umbral;
    Random rand = new Random();
    
    public perro(){
        animo = rand.nextFloat() * (5+5)-5;
        umbral = rand.nextFloat() * (0-20);
    }
    
    public Float getAnimo(){
        return animo;
    }
    
    public Float getUmbral(){
        return umbral;
    }
    
    private void psican(persona p){
        this.animo = (rand.nextFloat() -0.5) * 10 + this.animo;
        if (p.tieneGalleta()){
            p.darGalleta();
            this.animo += (1.0 / p.getConfiabilidad()) * (rand.nextInt(2));
        }
    
    System.out.println("Mi nuevo animo es: " + this.animo);
    }

I received this error:我收到了这个错误:

 "error: incompatible types: double cannot be converted to Float
            this.animo = (rand.nextFloat() -0.5) * 10 + this.animo;"

I have checked everything however, everything was stated as a float from the beginning.我已经检查了所有内容,但是从一开始就将所有内容都表示为浮点数。

Because 0.5 is a double (not a float ).因为0.5double (不是float )。 Change this改变这个

this.animo = (rand.nextFloat() -0.5) * 10 + this.animo;

to

this.animo = (rand.nextFloat() -0.5f) * 10 + this.animo;

or或者

this.animo += (rand.nextFloat() - 0.5f) * 10;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么 i = i + i 给我 0? - Why does i = i + i give me 0? 为什么我的方法说它必须返回一个类型字符串并给我一个错误,即使我的方法确实返回一个类型字符串 - Why does my method say it must return a type string and gives me an error even though my method does return a type string 获取FileNotFoundException,即使我声明它被抛出 - Getting FileNotFoundException even though I declared it to be thrown 为什么即使我没有运行错误,应用程序也不显示任何内容? - Why does the app displays nothing even though i have no running error? 即使我已经将Face类声明为final类,它似乎也不是一成不变的,该如何纠正呢? - My Face class does not seem to be immutable even though I already declared it as final, how do I correct it? 为什么即使日期相同,daes date1.compareTO(date2)== 0也不给我我想要的东西? - Why does daes date1.compareTO(date2)==0 even if dates are the same doesnt give me what i want? 如果多次调用此方法,为什么有时会给我一个错误 - Why does this method sometimes give me an error if I call it more than once 如果我输入一个未在数组中定义的值,为什么二进制搜索方法会给我一个错误? - Why does the binary search method give me an error if i put in a value that is not defined in the array? 为什么 hibernate 在我放置 transient 标签时会给我这个错误? - Why does hibernate give me this error when I put the transient tag? 为什么Long无法接受12位数值,即使我明确宣布它? - Why is Long unable to accept 12 digit value even though I explicitly declared it to?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM