Can i change value of final int

Arjun Krishnan

I am new to android and I am trying to create a game. It is a very simple Guess the number game.I want to change the value of the correct answer if he user guesses correctly. I am unsure how to do this. his is my code for on create:

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){
                tv1.setText("Sorry,Your guess is too low");
            }}
        }
    });
}

How do I change correctAnswer? I am forced to call it final and can't change the value.

kalyan pvs
Can i change value of final int??

For this answer is NO..

I can understand your problem you are using it in anonymous inner class so eclipse forcefully ask you to make it final..So you want to use correctAnswer value within anonymous inner class so remove the final and define your correctAnswer globally like ..

private int correctAnswer;

then you can change value and you can access it in anonymous inner class

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

How can I change button value with stylish?

분류에서Dev

Why can't I change the value in GridView?

분류에서Dev

JAVA: Parsing String to static final int value

분류에서Dev

Can I change asp.net mvc 5 identity user ID from string(GUID) to int

분류에서Dev

How can I get and change a value of a line inside of a file if the value of such line is unknown by me?

분류에서Dev

How can I automatically change the CSS min-height value of a div when a JavaScript is performed?

분류에서Dev

How can I change a wpf Databound grid cell based on the cell value?

분류에서Dev

Can I change between OSes?

분류에서Dev

Can I rename a table without breaking the final-release?

분류에서Dev

Can't change value on update with JOIN

분류에서Dev

If I have apply(i: Int), how can I have map?

분류에서Dev

In EF6 Code First can I change a navigation property value, then break the relationship, and have both changes saved in one transaction?

분류에서Dev

How do I change this value using regex?

분류에서Dev

How can I change the prompt of the terminal permanently

분류에서Dev

How can I change the default gateway?

분류에서Dev

Docky - how can I change jDownloader icon?

분류에서Dev

How can i change hyperlink color with JQuery?

분류에서Dev

Can I change the user that is the default choice in UAC?

분류에서Dev

How can I change the default Nautilus Bookmarks?

분류에서Dev

How can I change the colors of the slices of the pie?

분류에서Dev

Why can I not change user interface language?

분류에서Dev

Can the volume change when I plug in headphones?

분류에서Dev

How can I run a script on resolution change?

분류에서Dev

Can I change how file completion is displayed?

분류에서Dev

How can I change the system font?

분류에서Dev

Can't set a default value to INT(11) altering table

분류에서Dev

How can I display the latest value in Chart?

분류에서Dev

Can I add a JRadioButton for each value in an enum?

분류에서Dev

How can I test for a file having a value?

Related 관련 기사

  1. 1

    How can I change button value with stylish?

  2. 2

    Why can't I change the value in GridView?

  3. 3

    JAVA: Parsing String to static final int value

  4. 4

    Can I change asp.net mvc 5 identity user ID from string(GUID) to int

  5. 5

    How can I get and change a value of a line inside of a file if the value of such line is unknown by me?

  6. 6

    How can I automatically change the CSS min-height value of a div when a JavaScript is performed?

  7. 7

    How can I change a wpf Databound grid cell based on the cell value?

  8. 8

    Can I change between OSes?

  9. 9

    Can I rename a table without breaking the final-release?

  10. 10

    Can't change value on update with JOIN

  11. 11

    If I have apply(i: Int), how can I have map?

  12. 12

    In EF6 Code First can I change a navigation property value, then break the relationship, and have both changes saved in one transaction?

  13. 13

    How do I change this value using regex?

  14. 14

    How can I change the prompt of the terminal permanently

  15. 15

    How can I change the default gateway?

  16. 16

    Docky - how can I change jDownloader icon?

  17. 17

    How can i change hyperlink color with JQuery?

  18. 18

    Can I change the user that is the default choice in UAC?

  19. 19

    How can I change the default Nautilus Bookmarks?

  20. 20

    How can I change the colors of the slices of the pie?

  21. 21

    Why can I not change user interface language?

  22. 22

    Can the volume change when I plug in headphones?

  23. 23

    How can I run a script on resolution change?

  24. 24

    Can I change how file completion is displayed?

  25. 25

    How can I change the system font?

  26. 26

    Can't set a default value to INT(11) altering table

  27. 27

    How can I display the latest value in Chart?

  28. 28

    Can I add a JRadioButton for each value in an enum?

  29. 29

    How can I test for a file having a value?

뜨겁다태그

보관