不能解决“局部变量需要是最终的”

时间:2018-10-02 23:41:36

标签: java android variables

您好,我试图根据用户请求从数据库中添加计算结果,但是除非最终确定,否则浮点数将不起作用,如何解决此问题?

代码:

float v = (float) 0;
    StringBuilder upcoming = new StringBuilder("Upcoming\n\n");
    StringBuilder Results = new StringBuilder("Upcoming\n\n");
    switchExam.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(!isChecked)
            {
                for(FAssessment a : assessmentList) {
                    if(!a.getType().equals("Final"))
                    {
                        double d = CalculatePercentage(a.getTotal(),a.getAchieved(),Double.parseDouble(a.getWeight()));
                        v += (float) d;
                    }                        
                }
            }
            else
            {
                for(FAssessment a : assessmentList) {
                    double d = CalculatePercentage(a.getTotal(),a.getAchieved(),Double.parseDouble(a.getWeight()));
                    v += (float) d;
                }
            }
        }
    });

这是我遇到错误v += (float) d;

的地方

变量'v'是从内部类内部访问的,需要是最终的或实际上是最终的

2 个答案:

答案 0 :(得分:2)

也许谁保留了什么?

StringBuilder upcoming = new StringBuilder("Upcoming\n\n");
StringBuilder Results = new StringBuilder("Upcoming\n\n");

CompoundButton.OnCheckedChangeListener lister = new CompoundButton.OnCheckedChangeListener() {

    float v = 0.0;
    public float getV() { return v; }

    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(!isChecked)
        {
            for(FAssessment a : assessmentList) {
                if(!a.getType().equals("Final"))
                {
                    double d = CalculatePercentage(a.getTotal(),a.getAchieved(),Double.parseDouble(a.getWeight()));
                    v += (float) d;
                }                        
            }
        }
        else
        {
            for(FAssessment a : assessmentList) {
                double d = CalculatePercentage(a.getTotal(),a.getAchieved(),Double.parseDouble(a.getWeight()));
                v += (float) d;
            }
        }
    }
};
switchExam.setOnCheckedChangeListener(lister);

然后当封闭程序需要知道它可以调用什么v

lister.getV();

注意:除了将0强制转换为浮点数外,您还可以说类似以下内容:

float v = 0f;

答案 1 :(得分:2)

据我了解,您想稍后再使用此变量v。如果是这样,您可以获取所需的代码并将其放入方法中。这样,您可以在listener中声明变量,这将为变量提供正在寻找的final

赞:

switchExam.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            StringBuilder upcoming = new StringBuilder("Upcoming\n\n");
            StringBuilder Results = new StringBuilder("Results\n\n");
            float v= 0;
            if(!isChecked)
            {

                for(FAssessment a : assessmentList) {
                    if(!a.getType().equals("Final"))
                    {
                        double d = CalculatePercentage(a.getTotal(),a.getAchieved(),Double.parseDouble(a.getWeight()));
                        v += (float) d;
                    }

                    upcoming.append(a.getDesc()+" "+a.getDate()+"\n");
                    Results.append(a.getDesc()+" "+format.format(((a.getAchieved()/a.getTotal())*100))+"%\n");
                }                    
                SetInit(v,upcoming,Results);
            }
            else
            {
                for(FAssessment a : assessmentList) {
                    double d = CalculatePercentage(a.getTotal(),a.getAchieved(),Double.parseDouble(a.getWeight()));
                    v += (float) d;
                    upcoming.append(a.getDesc()+" "+a.getDate()+"\n");
                    Results.append(a.getDesc()+" "+format.format(((a.getAchieved()/a.getTotal())*100))+"%\n");
                }
                SetInit(v,upcoming,Results);

            }
        }
    });
    switchExam.setChecked(true);

然后在您的方法中,您将使用变量v粘贴您想要做的事情:

private void SetInit(float v, StringBuilder coming, StringBuilder results) {
    txtUpcoming.setText(coming);
    txtResults.setText(results);
    //And whatever you want to do with `v`
 }

希望这会有所帮助。 :)