2013-03-02 53 views
0

基本上之間的數字的ArrayList,我需要創建一個名爲方法:創建含有一組用戶定義的最小值和最大值(JAVA)

public boolean doesContain(double x) 

其工作方式如下:

Returns true if and only if x lies between left and right, and the 
interval is not empty (i.e. left < right). 

這是類的樣子:

public class Interval { 

    private double left; 
    private double right; 

    public Interval(double left, double right){ 
     this.left = left; 
     this.right = right; 
    } 

    public boolean doesContain(double x){ 

     ArrayList<Double> nums = new ArrayList<Double>(); 
     //Add code here 

    } 
} 
+1

您遇到什麼問題? – Reimeus 2013-03-02 15:24:55

+0

我想創建一個給定範圍內的數字列表,然後放入數組...但我真的不知道如何生成這些數字。 – 2013-03-02 15:28:26

+1

你正在使用雙打,理論上在2雙打內有無窮的數字,所以你不能列出所有的數字。 – niculare 2013-03-02 15:38:12

回答

1
public boolean doesContain(double x) { 
    if (left >= right) { 
     return false; 
    } else { 
     return x >= left && x <= right; 
    } 
} 
+1

如果'left> = right'則返回false。只有當'left 2013-03-02 15:33:39

1
public boolean doesContain(double x){ 
    return left < right && left <= x && x <= right; 
} 
+0

如果'x = left = right'則返回true,而不是OP想要的。 – 2013-03-02 15:29:44

+0

如何檢查間隔是否爲空? – 2013-03-02 15:30:56

+0

你是對的,現在它應該做的伎倆。 – niculare 2013-03-02 15:32:47

相關問題