从另一个类

时间:2016-04-03 00:48:09

标签: java

我在从两位数字类调用方法时创建一个四位数的类(如时钟)。

四位数类有两个部分。当段1达到我设定的最大值时,第二段必须增加1。

这是我第一堂课的方法:

/*
 * method to set the value
 */
public void setValue(int anyValue){
    if((anyValue < TOO_HIGH) && (anyValue >= 0)){
        value =  anyValue;}
}

/*
 * method to set the value plus one
 */
public void setValuePlusOne(){
    int tempValue = value + 1;
    if(tempValue < TOO_HIGH){
        value = tempValue;}
    else{
        value = 0;

        // value = tempValue % TOO_HIGH;}

    }

这是我的第二个四位数课程。

/*
 * method to set the increment
 */
public void setIncrement(int increment){
    rightSegment.setValuePlusOne();
    if(increment == 0)
        leftSegment.setValuePlusOne();  

    }

我认为我的增量可能有问题== 0.当我尝试时它没有编译 如果(rightsegment.setValuePlusOne()== 0)

任何建议都会有所帮助。谢谢!!

2 个答案:

答案 0 :(得分:1)

setValuePlusOne(...)不返回任何内容。在if之前调用setValuePlusOne然后使用(rightsegment.getValue()== 0)为if。

答案 1 :(得分:0)

请尝试以下代码。希望以下代码可以帮助您实现您的实现。 您可以在分别扩展Clock类的RightSegment和LeftSegment类中设置TOO_HIGH整数值,而不是在下面给定代码的if else块中设置TOO_HIGH整数值。 感谢

package stackoverflow;

public class Clock {

private int value;
private int TOO_HIGH;
private Clock rightSegment;
private Clock leftSegment;


/*
 * method to set the value
 */
public void setValue(int anyValue, String position){
    if(position.equals("right")){
        TOO_HIGH = 60;
    }else if(position.equals("left")){
        TOO_HIGH = 13;
    }

    if((anyValue < TOO_HIGH) && (anyValue >= 0)){
        value =  anyValue;}
}

/*
 * method to set the value plus one
 */
public void setValuePlusOne(){
    int tempValue = value + 1;
    if(tempValue < TOO_HIGH){
        value = tempValue;}
    else{
        value = 0;
    }

        // value = tempValue % TOO_HIGH;}

    }


    /*
     * method to set the i`ncrement
     */
    public void setIncrement(int increment, Clock right, Clock left){
        rightSegment = right;
        leftSegment = left;
        //rightSegment = new Clock();
        //leftSegment = new Clock();
        rightSegment.setValuePlusOne();
        if(increment == 0){
            leftSegment.setValuePlusOne();  
        }

    }

    public static void main (String args[]){
        Clock clock = new Clock();
        clock.rightSegment = new Clock();
        clock.leftSegment  = new Clock();
        clock.rightSegment.setValue(12, "right");
        clock.leftSegment.setValue(12, "left");
        clock.rightSegment.setIncrement(0, clock.rightSegment, clock.leftSegment);
        System.out.println("Time "+clock.leftSegment.value+":"+clock.rightSegment.value);
    }

}