The method newRandom() is undefined for the type Weather

The method newRandom() is undefined for the type Weather

错误提示:Exception in thread "main" java.lang.Error: Unresolved compilation problem: 

The method newRandom() is undefined for the type Weather


at com.imooc.weather.Weather.<init>(Weather.java:9)

at com.imooc.weather.WeatherTest.main(WeatherTest.java:6)

应该如何修改哦?



package com.imooc.weather;


public class WeatherTest {


public static void main(String[] args) {

Weather oneday=new Weather();

oneday.generate();

//oneday.read();


}


}



package com.imooc.weather;

import java.util.Random;


public class Weather {

private int temperature;



private int humdity;

boolean flag = false;

Random rand = newRandom();


public synchronized void generate() {

this.setTemperature(rand.nextInt(41));

this.setHumdity(rand.nextInt(101));

System.out.println("生成"+this);

}

public synchronized void read() {

System.out.println("读取"+this);

}

@Override

public String toString() {

return "天气数据 [温度:"+this.getTemperature()+",湿度"+this.humdity+"]";

}

public int getTemperature() {

return temperature;

}

public void setTemperature(int temperature) {

this.temperature = temperature;

}

public int getHumdity() {

return humdity;

}

public void setHumdity(int humdity) {

this.humdity = humdity;

}

}


正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,同学如下位置中间少个空格,new和Random连起来了,所以导致报错

通过给中间加个空格试试

http://img1.sycdn.imooc.com//climg/60deada709ceebef03400067.jpg

祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师