-4

所以我完全不知道如何做到这一点,任何帮助将不胜感激,我想做的基本上是得到两个整数之间的所有数字。

所以说我有数字:254 和 259 我想输出以下数字:255、256、257、258

我还想将这些数字添加到一个列表中,并能够输出该列表中有多少个数字,所以在这种情况下,列表中有 4 个数字。

我正在用它来循环穿过一片土地。

4

4 回答 4

2
public static List<Integer> getOpenRange(int start, int end) {
    List<Integer> result = new ArrayList<>();
    for (int i = start + 1; i < end; ++i)
        result.add(i);
    return result;
}
于 2013-08-24T02:24:23.480 回答
2
int[] array = new int[max-min]; 

for (int i = min + 1; i < max; i++)
{
    array[i - min - 1] = i;
}
于 2013-08-24T02:26:00.617 回答
2

试试这个...

public static void main(String[] args) throws Exception {

    int start = 254;
    int end = 259;
    List<Integer> numberList = new ArrayList<Integer>();
    for(int i = start+1; i < end; i++) {
        //Prints the numbers exclusive...
        System.out.println(i);
        //Adds the numbers to the list
        numberList.add(i);          
    }

    //Prints the length of the list.
    System.out.println("Size " + numberList.size());

}
于 2013-08-24T02:28:28.680 回答
1

鉴于:

int min, max;

循环输出:

for (int i = min + 1; i < max; i++)
    System.out.println(i);

要确定大小,您不需要列表:

int size = max - min - 1;
于 2013-08-24T03:49:21.277 回答