2017-04-07 13 views
0

我试图制作一个程序,在该程序中输入一定的天数和起始温度。温度在整个天数内以某种方式变化。然后打印最后一天的温度。我的教授说要使用类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提前。

+0

主要方法应该是静态的。 – Omore

回答

0

有几件事情马车在这里,这需要改变:

  • 主要方法应该和必须是静态的。
  • findFinalDay()方法应该是静态的,以便从main()方法调用它。
  • TemperaturePattern类不应该是公开的,因为它打算用作内部类(根据我的理解)。

查找以下修改后的代码:

import java.util.Scanner; 
public class HWfive{ 
     public static void findFinalDayTemperature(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 static 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(); 
      findFinalDayTemperature(days,temp); 
     } 
class TemperaturePattern{ 
    int num_of_days = 0; 
    int temp_first_day = 0; 
    public void TemperaturePattern(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; 
     } 
     } 
} 

的静态方法说明:在一类被加载时被创建static方法或可变的。只有在例如使用new运算符将该类实例化为对象时,才会创建未声明为static的方法或变量。

此外,Java虚拟机不会创建类的实例,而只是在编译时加载并在main()方法中开始执行时,必须使用static修饰符声明main()方法,以便只要该类为加载,main()方法可用。

不具备static修改这些变量,哪些是该main()法外之类的方法不能使用,直至类的实例已经为main()方法中的对象创建的,所以你的情况方法'findFinalDayTemperature()'必须是静态的,以便通过'main()'方法调用它。

+0

非常感谢你帮助很多〜你介意解释为什么这些方法必须是静态的吗? – JNV

+0

@JNV我很高兴它有帮助。请在静态修改器上找到我的更新。 – dildeepak

+0

@JNV另外,如果它有帮助,请投票回答。 – dildeepak