私は一定の日数と開始温度の入力を得るプログラムを作ろうとしています。温度は日々のある一定の方法で変化します。その後、最終日の温度を表示します。私の教授は、クラスTempPattern、fields 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)
私は何かがnull値だと思いますが、これを修正する方法は本当に分かりません。私はまた、コンストラクタやフィールドのものを正しく作ったとは思わないので、助けやアドバイスを自由にしてください。私は理にかなっていないものはすべて解決します。 TYを事前に入力してください。
メインメソッドは静的である必要があります。 – Omore