变量未初始化并隐藏在字段中 [英] Variable not initialized and hides in field

查看:41
本文介绍了变量未初始化并隐藏在字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对Java还是很陌生,所以我不明白为什么我会收到初始化错误.在代码中,我的整数"Memes"表示可能尚未初始化,并且局部变量隐藏在字段中.

I'm fairly new to java, so I don't understand why i'm getting an initialization error. In the code my integer "Memes" is saying that it might not have been initialized and that a local variable hides in field.

 private void totalUpdate(int yes){


     if (yes==1){
             int CompBot = Integer.parseInt(lblbotComp.getText());
     int CompTop = Integer.parseInt(lbltopComp.getText());
     int CompMid = Integer.parseInt(lblmidComp.getText());

                int Memes = Memes + CompBot + CompTop + CompMid;
          lbltotalComp.setText("Computer has earned "+ Memes +" points in total");
}
    }
 private void pointUpdate(int points){
     pointsUser = pointsUser + points ;
     lbluserPointsEst.setText (""+pointsUser+"");
 }

 private void computerPointUpdate(int pointComp){
     pointsComputer = pointsComputer + pointComp ;
     lblcompPointsEst.setText (""+pointsComputer+"");
 }

 private int play(int points){
int score;
int randomScore = (int)((100 -1 +1)*Math.random() +1);
 if (randomScore < points){
     score = points;
     }
    else{
   score = 100;
    }

return score;
 }

 private int playComp(int pointsComp){
int score;
int randomScore = (int)((100 -1 +1)*Math.random() +1);
 if (randomScore < pointsComp){
     score = pointsComp;
     }
    else{
   score = 100;
    }

  return score;
 }

        private int Memes = 0;
        private final Timer messageTimer;
        private int pointsComputer = 0;
        private int pointsUser = 0;
        private int count;

推荐答案

执行此操作

int Memes = Memes + CompBot + CompTop + CompMid;

Java将此视为对新变量的声明,该变量隐藏了字段

Java treats this as a declaration of a new variable, which hides field

private int Memes = 0;

您似乎想初始化该字段的 Memes .为此,请将 int 放在作业前面:

It looks like you wanted to initialize Memes which is the field. In order to do that, drop int in front of the assignment:

Memes = Memes + CompBot + CompTop + CompMid;

这篇关于变量未初始化并隐藏在字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆