2013-06-24 90 views
-3

我的對象如下所示:請注意,這不是家庭作業,而是我自己正在做的Web項目。所以幫助將不勝感激!返回與數組中連續序列相匹配的第一類索引

enum size { small, big; } 

Class Controller 
{ 
    size sizeType; 
} 

Class square extends shape 
{ 
    int num = 1; 
} 

Class circle extends shape 
{ 
    int num=2; 
    size size = size.Small; 
} 

void method() 
{ 
    Controller[] sizes = new Controller[n]; 
    // e.g. shape = {small, big, small, small, big} 

    Shape circle = new Shape(); 
    /* Find 2 'small' continuous circles 
     OR find 'size' based on the num value 
     (circle has 2 but should be able to accept 
     any integer = num declared in the shape class */ 

    // RETURN occurrence of first such index for e.g. 2 as found in 2,3  
} 
+5

什麼問題? – egrunin

+0

你的「Shape」類在哪裏? – AJMansfield

+0

「type」枚舉在哪裏? – AJMansfield

回答

1

你的代碼真正的問題:你完全破壞對象的層次結構。

首先第一件事情,這裏有一些小的修正,根據我的猜測,你打算什麼:

enum SizeType { SMALL, BIG; } 

class Shape { 
    SizeType size; 
} 

class Square extends Shape { 
    int num = 1; 
} 

class Circle extends Shape { 
    int num = 2; 
    SizeType size = SizeType.SMALL; 
} 

int findShapeSequence(Shape[] shapes) { 
    // TODO find the first instance of a repeated size value in the array, 
    // and return the index of the first of the shapes whose size repeats. 
} 

現在,這個被清除,它應該很容易爲你找出如何實際上做你想要的方法。

+0

如何找到2個類型爲小圓的連續形狀序列? – Phoenix

+0

@Phoenix這是你的工作,搞清楚。我會幫助你,但只有當你真正嘗試自己解決它時纔會幫助你。提示:使用'for'循環遍歷形狀,並將當前'Shape'的'size'值與下一個值的'size'值進行比較。 – AJMansfield

+0

但形狀的數量可以是1/2/3,但不是固定的。對於例如如果有5 ..有一個更簡單的方法來做到這一點? – Phoenix

相關問題