dr.java中的空指针异常
我试图制作一个程序,在那里我可以输入一定的天数和起始温度。温度在一天中以某种方式变化。然后打印最后一天的温度。我的教授说使用类TempPattern、字段num_days和first_day_temp以及构造函数和finalTemp方法。以下是我所拥有的:dr.java中的空指针异常,java,drjava,Java,Drjava,我试图制作一个程序,在那里我可以输入一定的天数和起始温度。温度在一天中以某种方式变化。然后打印最后一天的温度。我的教授说使用类TempPattern、字段num_days和first_day_temp以及构造函数和finalTemp方法。以下是我所拥有的: public class TempPattern{ int num_of_days = 0; int temp_first_day = 0; public void TempPattern(int temp,
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);
}
我认为有些东西是空值,但我真的不知道如何修复它。我也认为我没有正确地完成整个构造函数和字段的工作,所以请随时提供任何帮助/建议,我非常感谢。我会清理一切不合理的东西。提前完成。这里有两件事需要改变:
- 主方法应该而且必须是静态的
- findFindFinalDay()方法应该是静态的,以便从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;
}
}
}
import java.util.Scanner;
公共类HW5{
公共静态无效FindFindFindayTemperature(整数天,整数温度){
int半=天/2;
int newtemp=温度+2;
对于(int current_day=1;current_day main method应该是静态的。非常感谢您的帮助~您介意解释为什么这些方法必须是静态的吗?@JNV我很高兴它起到了作用。请查找我对静态修饰符的更新。@JNV另外,如果它有帮助,请投票决定答案。
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;
}
}
}