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

 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;

最佳答案

之所以收到此警告,是因为您的类定义中有private int Memes,并且在int Memes中有声明的变量totalUpdate()

如何解决它取决于您想要的:

如果要totalUpdate()更新对象中Memes的值,请在相关行中省略int,如下所示:

Memes = Memes + CompBot + CompTop + CompMid;

如果要声明一个新的局部变量,最好给它一个不同的名称:

int localMemes = Memes + CompBot + CompTop + CompMid;

10-04 14:07