2017-09-20 121 views
-1

我一直在尝试创建一个代码,用于在用户输入特定数据时计算windchill。我是Java的新手,并且在这个项目中遇到了很多困难。但是,我终于成功运行了它。即使它现在正在运行,我仍然收到消息说我的变量可能是最终的。我不确定这意味着什么,也不能让它消失。我在评论中标记了那些对他们有消息的人。有人可以解释这是什么意思,为什么发生?什么是Netbeans中的变量可以是最终的意思?

这里是我的代码:

import java.util.Scanner; 

public class Main { 

    public static void main(String[] args) { 
     String line; 
     String date; 
     int windSpeed, temperature; 

     Scanner kb = new Scanner(System.in); 
     System.out.println("Enter the observation date: "); 
     date = kb.nextLine(); 

     System.out.println ("Enter the wind speed in MPH: ");   
     line = kb.nextLine(); 
     windSpeed = Integer.parseInt(line); 

     System.out.println ("Enter the temperature in degrees F: "); 
     line = kb.nextLine(); 
     temperature = Integer.parseInt(line); 

     Observation observation1 = new Observation(date,windSpeed,temperature); 
     System.out.println("Observation Date: " + observation1.getDate()); 
     System.out.println("WindChill: " + observation1.getwindChill()); 

    }  
} 

我在同一个包中的其他类:

在Java中
public class Observation { 
    private String date; //can be final 
    private int windSpeed;//can be final 
    private int temperature;//can be final 
    private double windChill;//can be final  

    public Observation (String d, int s, int t){ 
     date = d; 
     windSpeed = s; 
     temperature = t; 

     windChill = 35.74 + 0.6215*t + (0.4275*t - 35.75) * Math.pow(s,0.16); 

    } 

    public String getDate(){ 
     return date; 
    } 

    public double getwindChill(){ 
     return windChill; 
    } 
} 
+1

你将它们设置在构造函数中,并且只在那里。这意味着他们以后不会改变,并且表现得像最终一样。 –

回答

2

最后的变量是costants,这意味着,一旦你设定自己的价值,它永远不会改变。 NetBeans将建议你把一个final修改您的变量的声明时:

  • 您创建后分配一个值
  • 您初始化它们在构造

,你永远不改变自己值。