简体   繁体   中英

keeping a value constant from one method to an overloaded method

I am currently taking an introduction to java course and I'm stumped here..

I created a rollDice() method that uses Math.random to produce 2 int type values stored in variables d1 and d2 . If the sum of the values equal 4,6,8,9,10,or 11 a valuePoint is established of that sum, and then the overloaded method play(int d1, int d2) is called where the dice is rolled again until either the sum equals 7(you lose) or the sum equals the original valuePoint established in the first method.

My problem is I cant seem to keep the original valuePoint constant because it is first established as valuePoint = sum . So every time I roll the dice in the second method the valuePoint changes again to equal sum. I hope I explained my problem well.

public void play(){
    rollDice();
    sum = d1 + d2;
    if(sum == 2 || sum == 3 || sum == 5) {
        status = "You Lose.";
        valuePoint = 0;
        System.out.println(status);
    }
    else if(sum == 7 || sum == 11) {
        status = "You Win!";
        valuePoint = 0;
        System.out.println(status);
    }
    else {
        status = "You established the value point ";
        valuePoint = sum;
        System.out.println(status + valuePoint);
    }
    if(valuePoint == sum)
        play(d1,d2);
} // end of method


public void play(int d1, int d2){

    final int vp = valuePoint;
    sum = 0;
    start = true;
    while (true) {
        System.out.println("Roll again..");
        rollDice();
        sum = d1 + d2;
        if (sum == vp) {
            status = "You Win!";
            System.out.println(status);
            start = false;
            break;
        }
        else if (sum == 7) {
            status = "You Lose.";
            System.out.println(status);
            start = false;
            break;
        }
    } // end of while
} // end of method

Here is a sample of my output..

You rolled 1 + 5 = 6
You established the value point 6  
Roll again..
You rolled 2 + 6 = 8  
You Win! 

Notice that second roll did not reach the valuepoint , but its still a win because the valuePoint has been changed to the new sum.

Also the attributes and operations are given in the question so I have to follow this API to achieve full marks. The only variable I added to the attributes was the final int vp in an attempt to keep that darn valuePoint constant when first established but this did not work.

The issue is actually related to scope. In your method play(int d1, int d2) , when you refer to d1 and d2 in the line sum = d1 + d2; you are actually referring to the parameters of the function, not the instance variables since the parameters have a more "local scope". Thus, you are never actually updating a sum with two new values rolled. You can fix this by either changing the names of the parameters so they aren't the same as the instance variables or by referring to your instance variables as this.d1 and this.d2 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM