使用while循环的Java数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19480304/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 17:36:35  来源:igfitidea点击:

Java array using a while loop

javaarrays

提问by user1534779

I have to:

我必须:

  • create this java array;
  • loop through it using a while loop;
  • terminate the program if the sum adds up to 100;
  • and print the sum and the numbers that I did put into the array.
  • 创建这个java数组;
  • 使用 while 循环遍历它;
  • 如果总和为 100,则终止程序;
  • 并打印我放入数组的总和和数字。

I can't figure out how to do that, here is my code so far, any help would be appreciated.

我不知道如何做到这一点,这是我的代码到目前为止,任何帮助将不胜感激。

public class december2012 {
    public static void main(String[] args) {  

        int sum=0;            

        Scanner input = new Scanner(System.in);            

        int i=1;

        int [] array = new int[i];

        while( i > array.length || sum <= 100) {

          System.out.println("Write in the " + i + " number") ; 
          array[i]=input.nextInt();
          sum=+array[i];
           System.out.println("sum is " + sum); 

        }            

        }
    }

回答by Pradeep Simha

How about like this, simple way of doing:

像这样,简单的做法如何:

int sum = 0;
while(true) {
   //Do calculations
   if(sum >= 100) {
      break;
   }
}

System.out.println("Sum is : " + sum);

回答by Birb

First of all, when setting

首先,在设置时

int i=1;
int [] array = new int[i];

you are creating an array with 1 slot, which index is 0. This is crucial to take note of, when you're doing this:

您正在创建一个具有 1 个插槽的数组,其索引为0. 当您执行此操作时,请务必注意:

array[i]=input.nextInt();

because, as you defined it, iis not 1, which means that your assign the 2nd index, index 1to the return value of input.nextInt().

因为,当你定义它,i不为1,这意味着你的分配第二索引,索引1到的返回值input.nextInt()

Your while loop is also off, as i should never be bigger than the length of the array. You should use a for-loop instead, in this fasion:

您的 while 循环也已关闭,因为 i 永远不应大于数组的长度。在这种情况下,您应该改用 for 循环:

for(int i = 0; i < array.length; i++) {
    array[i] = input.nextInt();
    sum += array[i]

    if (sum > 100) {
        return;
    }
}

回答by canhazbits

You need to increment the ivariable; currently its value is always 0, so you're only setting the first element in the array.

您需要增加i变量;目前它的值总是0,所以你只设置数组中的第一个元素。

回答by Ilya

int i = 0;                            // array starts from 0
int [] array = new int[100];          // create larger array
while(i < array.length && sum <= 100) // i should be less then length
                                      // && instead of ||
{
   System.out.println("Write in the " + i + " number") ; 
   array[i] = input.nextInt();
   sum += array[i];                   // += instead of =+
   System.out.println("sum is " + sum);
   i++;                               // increment i 
}  

Ideone DEMO

演示版

回答by La-comadreja

public class December2012 {
    public static void main(String[] args) {
        int[] array = new int[100];
        int sum = 0;
        int i = 0;
        Scanner input = new Scanner(System.in);
        while (sum <= 100 && i < array.length) {
            System.out.print("Write in the " + (i + 1) + "th number: ");
            array[i] = input.nextInt();
            sum += array[i];
            i++;
        }
        System.out.print("You added: ");
        for (Integer i : array) System.out.print(i + " ");
        System.out.println("\nsum is " + sum);
    }
}