我试图制作一个程序,在该程序中输入一定的天数和起始温度。温度在整个天数内以某种方式变化。然后打印最后一天的温度。我的教授说要使用类TempPattern,字段num_days和first_day_temp以及构造函数和finalTemp方法。继承人我有什么:dr.java中的空指针异常
public class TempPattern{
int num_of_days = 0;
int temp_first_day = 0;
public void TempPattern(int temp, int days){
days = num_of_days;
temp = temp_first_day;
}
public int num_of_days(int days){
return days;
}
public int temp_first_day(int temp){
return temp;
}
}
public void finalDayTemp(int days, int temp){
int half = days/2;
int newtemp = temp + 2;
for (int current_day = 1; current_day <= half; current_day++){
newtemp = newtemp - 2;
}
for (int current_day = half + 1; current_day <= days; current_day++){
newtemp++;
}
System.out.println("Temperature on final day would be " + newtemp);
}
public void main(String[] args){
Scanner keyboard = new Scanner(System.in);
int days;
int temp;
System.out.print("number of days: ");
days = keyboard.nextInt();
System.out.print("temperature of first day: ");
temp = keyboard.nextInt();
finalDayTemp(days,temp);
}
它编译,但出现错误。
java.lang.NullPointerException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:267)
我认为东西是空值,但我真的不知道如何解决这个问题。我也不认为我做了整个构造函数和正确的东西,所以请随时提供任何帮助/建议,我很感激。我会清理任何没有意义的事情。 TY提前。
主要方法应该是静态的。 – Omore