[英]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.. 我目前正在介绍Java课程,但在这里很困惑。
I created a rollDice()
method that uses Math.random
to produce 2 int
type values stored in variables d1
and d2
. 我创建了一个
rollDice()
方法,该方法使用Math.random
生成2个存储在变量d1
和d2
int
类型值。 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. 如果值的总和等于4、6、8、9、10或11,
valuePoint
建立该总和的valuePoint
,然后调用重载方法play(int d1, int d2)
,将骰子再次滚动到其中一个总和等于7(您输了)或总和等于第一种方法中建立的原始valuePoint
。
My problem is I cant seem to keep the original valuePoint
constant because it is first established as valuePoint = sum
. 我的问题是我似乎无法保持原始的
valuePoint
不变,因为它首先被建立为valuePoint = sum
。 So every time I roll the dice in the second method the valuePoint
changes again to equal sum. 因此,每当我在第二种方法中掷骰子时,
valuePoint
再次更改为相等的总和。 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. 请注意,第二个掷骰没有达到
valuepoint
,但是仍然赢了,因为valuePoint
已更改为新的总和。
Also the attributes and operations are given in the question so I have to follow this API to achieve full marks. 问题中还给出了属性和操作,因此我必须遵循此API才能获得满分。 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. 我添加到属性中的唯一变量是最终的
int vp
,以试图在首次建立该值时保持该valuePoint
值不变,但这是行不通的。
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;
在您的方法
play(int d1, int d2)
,当您在sum = d1 + d2;
行中引用d1
和d2
时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
. 您可以通过更改参数名称(使其与实例变量不同)或通过将实例变量称为
this.d1
和this.d2
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.