我可以更改final int的值吗?

 刘少静mm_527 发布于 2023-02-08 15:23

我是Android的新手,我正在尝试创建一个游戏.这是一个非常简单的猜数字游戏.如果用户猜对了,我想改变正确答案的值.我不确定该怎么做.他是我的创建代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button subm = (Button) findViewById(R.id.button1);
    final TextView tv1 =(TextView) findViewById(R.id.textView1);
    final EditText userG=(EditText)findViewById(R.id.editText1);
    Random rand=new Random();
    final int correctAnswer=rand.nextInt(100)+1;
    subm.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            int userNum=Integer.parseInt(userG.getText().toString());
            if(userNum>100 || userNum<0){
                tv1.setText("Enter a number between 1 and 100");
            }
            else{
            if(userNum==correctAnswer){

                tv1.setText("YEAH! You got it right");
            }
            else if(userNum>correctAnswer){

                tv1.setText("Sorry,Your guess is too high");
            }
            else if(userNum

如何更改correctAnswer?我被迫称之为最终,不能改变价值.

1 个回答
  • Can i change value of final int??
    

    对于这个答案是否定的..

    我可以理解你在匿名内部类中使用它的问题所以eclipse强烈要求你让它成为最终的..所以你想correctAnswer在匿名内部类中使用值,所以删除final并定义你的correctAnswer全局像..

    private int correctAnswer;
    

    然后你可以改变值,你可以在匿名内部类中访问它

    2023-02-08 15:25 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有