为什么我需要在Java中为这个数组添加+ 1?

时间:2021-09-16 08:00:19

I'm looking at this piece of code I found and hopefully someone can help me. The program takes two numbers and prints to the screen what is in between the the two numbers, so for example 20 and 15 would print

我正在查看我找到的这段代码,希望有人可以帮助我。该程序采用两个数字并在屏幕上打印两个数字之间的内容,因此例如打印20和15

[15, 16, 17, 18, 19, 20]

[15,16,17,18,19,20]

I want to know why the person would wrote the code decided to add 1 to the array here:

我想知道为什么这个人会写代码决定在这里添加1到数组:

int[] range = new int[(upper - lower + 1)]; 

if you were to leave the +1 out the print statement would only produce

如果你要留下+1,那么print语句只会产生

[15, 16, 17, 18, 19]

[15,16,17,18,19]

Hopefully someone can help me out.

希望有人可以帮助我。

Actual desired program output : The array: [ 15 16 17 18 19 20 ]

实际所需的节目输出:阵列:[15 16 17 18 19 20]

public class RangeLister {

    int[] makeRange(int lower, int upper) {

        int[] range = new int[(upper - lower + 1)];

        for (int i = 0; i < range.length; i++) {
            range[i] = lower++;
        }
        return range;
    }

    public static void main(String[] arguments) {
        int[] range;
        RangeLister lister = new RangeLister();

        range = lister.makeRange(15, 20);
        System.out.print("The array: [ ");
        for (int i = 0; i < range.length; i++) {
            System.out.print(range[i] + " ");
        }
        System.out.print("]");
    }
}

4 个解决方案

#1


0  

The range needs to be inclusive if you want to print the first integer and every integer inbetween including the last integer. Adding the +1 makes the range inclusive.

如果要打印第一个整数和中间的每个整数(包括最后一个整数),则范围必须是包含的。添加+1会使范围包含在内。

It's easier to see with a smaller range like [1, 2].

像[1,2]这样的较小范围更容易看到。

You want to print:

你想打印:

1 2

Without adding the +1 the range would be

没有添加+1,范围就是

range = 2 - 1 = 1

Which means that in your for loop, you would only call the print 1 time. This would result in an output of:

这意味着在你的for循环中,你只能打印1次。这将导致输出:

1

By adding the +1 you are including the last number so your range is:

通过添加+1,您将包括最后一个数字,因此您的范围是:

range = 2 - 1 + 1 = 2

Now, in your for-loop, you will print 2 numbers and your ouput will be

现在,在你的for循环中,你将打印2个数字,你的输出将是

1 2 

#2


4  

Because you want your range to be inclusive.

因为您希望您的范围具有包容性。

If you left the +1 out, you would have 20-15 = 5. However, you want to include 20 and 15, so you need an extra digit. Just count them:

如果你将+1输出,你将有20-15 = 5.但是,你想要包括20和15,所以你需要一个额外的数字。算一下吧:

15, 16, 17, 18, 19, 20

That's 6 digits (20-15+1), not 5 digits (20-15).

这是6位数(20-15 + 1),而不是5位数(20-15)。

#3


0  

You need the value of range to be 6. so that it can iterate/pick 15 to 20.

您需要将range的值设置为6.以便它可以迭代/选择15到20。

Take the following statement. where upper=20 and lower=15.

请采取以下声明。其中upper = 20且lower = 15。

int[] range = new int[(upper - lower + 1)]; 
range= 20-15+1 = 6

if you do just `int[] range = new int[(upper - lower)];

如果只做`int [] range = new int [(upper - lower)];

range= 5. you miss the number '20'.

range = 5.你错过了数字'20'。

#4


0  

That's because there are actually 6 numbers from [15,20]. The difference of these numbers would only give 5 which will not print numbers up to the greater number i.e 20. Adding +1 solves the problem.

那是因为[15,20]实际上有6个数字。这些数字的差异只会给出5不会打印数字到更大的数字,即20.添加+1解决问题。

#1


0  

The range needs to be inclusive if you want to print the first integer and every integer inbetween including the last integer. Adding the +1 makes the range inclusive.

如果要打印第一个整数和中间的每个整数(包括最后一个整数),则范围必须是包含的。添加+1会使范围包含在内。

It's easier to see with a smaller range like [1, 2].

像[1,2]这样的较小范围更容易看到。

You want to print:

你想打印:

1 2

Without adding the +1 the range would be

没有添加+1,范围就是

range = 2 - 1 = 1

Which means that in your for loop, you would only call the print 1 time. This would result in an output of:

这意味着在你的for循环中,你只能打印1次。这将导致输出:

1

By adding the +1 you are including the last number so your range is:

通过添加+1,您将包括最后一个数字,因此您的范围是:

range = 2 - 1 + 1 = 2

Now, in your for-loop, you will print 2 numbers and your ouput will be

现在,在你的for循环中,你将打印2个数字,你的输出将是

1 2 

#2


4  

Because you want your range to be inclusive.

因为您希望您的范围具有包容性。

If you left the +1 out, you would have 20-15 = 5. However, you want to include 20 and 15, so you need an extra digit. Just count them:

如果你将+1输出,你将有20-15 = 5.但是,你想要包括20和15,所以你需要一个额外的数字。算一下吧:

15, 16, 17, 18, 19, 20

That's 6 digits (20-15+1), not 5 digits (20-15).

这是6位数(20-15 + 1),而不是5位数(20-15)。

#3


0  

You need the value of range to be 6. so that it can iterate/pick 15 to 20.

您需要将range的值设置为6.以便它可以迭代/选择15到20。

Take the following statement. where upper=20 and lower=15.

请采取以下声明。其中upper = 20且lower = 15。

int[] range = new int[(upper - lower + 1)]; 
range= 20-15+1 = 6

if you do just `int[] range = new int[(upper - lower)];

如果只做`int [] range = new int [(upper - lower)];

range= 5. you miss the number '20'.

range = 5.你错过了数字'20'。

#4


0  

That's because there are actually 6 numbers from [15,20]. The difference of these numbers would only give 5 which will not print numbers up to the greater number i.e 20. Adding +1 solves the problem.

那是因为[15,20]实际上有6个数字。这些数字的差异只会给出5不会打印数字到更大的数字,即20.添加+1解决问题。