如果2个变量具有相同的状态,则更改变量的状态

时间:2015-01-10 18:30:52

标签: java

我不确定我的头衔很好地解释了我的问题,

基本上我有两个变量: -

public int driverNumberOne;
public int driverNumberTwo;

首先用户将driverNumberOne设置为int值,然后将driverNumberTwo设置为另一个int值,但我想要它做什么,当用户输入例如driverNumberTwo的int值时,我希望该方法将driverNumberOne的值更改为一个不同的值,所以我得到2变量具有相同的int值。 到目前为止,这是我的基本方法标题和正文,花了很多年时间试图弄清楚要做什么:-(

/**
 * Allows the user to change the number for driver one.
 */
public void setDriverNumberTwo(int aDriverNumber)
{
   this.setDriverNumberTwo(aDriverNumber);

3 个答案:

答案 0 :(得分:0)

public int driverNumberOne; 
public int driverNumberTwo;
public void setDriverNumberTwo(int aDriverNumber) {
    this.setDriverNumberTwo(aDriverNumber);
    if (this.driverNumberOne == this.driverNumberTwo) {
        ... change the value of the second variable to whatever ...
    }
}

答案 1 :(得分:0)

public void setDriverNumberTwo(int aDriverNumber )
{
    this.setDriverNumberTwo(aDriverNumber);
    if (this.driverNumberOne == this.driverNumberTwo) {
        // Set this.driverNumberOne to any Random Number 
    }
}

答案 2 :(得分:0)

我认为你写的方法会导致无限循环..

public void setDriverNumberTwo(int aDriverNumber ) {
    this.setDriverNumberTwo(aDriverNumber);// <- delete this line if you don't want an infinite loop method
    ...
}

你可以做你想做的正确方法是:

public void setDriverNumberTwo() {
    if (driverNumberOne == driverNumberTwo)
        driverNumberOne++;
}