2016-04-03 44 views
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)

任何意見會有所幫助。謝謝!!

+1

'rightSegment.setValuePlusOne();'應該返回'int'進行編譯。 –

+0

請使用自動縮進和格式化您的代碼的IDE。你發佈的代碼有點混亂,特別是右花括號很混亂。 –

+0

那麼......爲什麼不使用兩個單獨的類,而不是將它們合併爲一個,並使用if語句來控制增量?對程序目的的更多瞭解將有助於指導您走向正確的方向。 –

回答

1

setValuePlusOne(...)不返回任何內容。在if之前調用setValuePlusOne,然後對if使用(rightsegment.getValue()== 0)。

0

請嘗試下面的代碼。希望下面的代碼能幫助你實現你的實現。 而不是在以下給定代碼的if else塊中設置TOO_HIGH整數值,您可以分別將它設置在正在擴展Clock類的RightSegment和LeftSegment類中。 由於

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); 
    } 

}