Int 数组通过 while 循环推送直到它到达 -1

Int array pushed through while loop until it reaches -1

我必须接受一行用户输入,并使用 while 循环计算所有数字的平均值,直到它达到 -1。用户输入的示例可能类似于 2 -1 6,这就是我这样做的原因。我已经想出如何将它拆分成一个 int 数组,但我不知道如何做 while 循环部分。

System.out.println("user input")
String user = scan.nextLine();
String[] string = user.split(" ");
int[] numbers = new int[string.length];
for(int i = 0;i < string.length;i++) {
    numbers[i] = Integer.parseInt(string[i]);
}
while ( > -1){
            
}
import java.util.Scanner; 

public class Main
{
    public static void main(String[] args) {
        
        Scanner scan = new Scanner(System.in); 
        System.out.println("user input");
        String user = scan.nextLine();
        boolean found = false;
        Double average = 0.0;
        
        
        String[] string = user.split(" ");
        int[] numbers = new int[string.length];
        for(int i = 0;i < string.length && found == false ;i++) {
            numbers[i] = Integer.parseInt(string[i]);
        }
        int t = 0;
        while (found == false && t < string.length){
              if(numbers[t] == - 1){
                average = average/t;
                found = true;
              }
              else{
                    average = (Double) average + numbers[t];
                    t++;
                  }            
        }
        System.out.println("Average = " + average);

    }
}

Class java.util.Scanner 有方法 hasNextIntnextInt.

import java.util.Scanner;

public class Averages {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter series of integers on single line separated by spaces.");
        System.out.println("For example: 2 -1 6");
        int sum = 0;
        int count = 0;
        while (scan.hasNextInt()) {
            int num = scan.nextInt();
            if (num == -1) {
                break;
            }
            sum += num;
            count++;
        }
        if (count > 0) {
            double average = sum / (double) count;
            System.out.println("Average: " + average);
        }
        else {
            System.out.println("Invalid input.");
        }
    }
}

请注意,在计算平均值时,您需要将 count 转换为双精度值,否则将执行整数除法并且不会给出正确的平均值。

我假设你的意思是,当用户输入数字为 -1 时。我们应该取-1之前所有数字的平均值。那就是我在这里做的。

System.out.println("user input")
String user = scan.nextLine();
int totalSum = 0;
double avg = 0;

String[] string = user.split(" ");
int[] numbers = new int[string.length];
for(int i = 0;i < string.length;i++) {
    numbers[i] = Integer.parseInt(string[i]);
    if(numbers[i]==-1){
        avg = (double)totalSum / i;
        break;
    }
    totalSum += numbers[i]; 
}

只有 while 循环

System.out.println("user input");
String user = scan.nextLine();
int totalSum = 0;
double avg = 0;

String[] string = user.split(" ");
int[] numbers = new int[string.length];
int i = 0;
numbers[i] = Integer.parseInt(string[i]);
while(numbers[i]!=-1) {
    totalSum += numbers[i];
    i++;
    numbers[i] = Integer.parseInt(string[i]); 
}
avg = (double)totalSum / i;