gpt4 book ai didi

java - 我的java数组代码有什么问题?

转载 作者:行者123 更新时间:2023-11-29 03:07:05 26 4
gpt4 key购买 nike

   import java.util.Scanner;


public class SumArray {

public static void main(String[] args) {


int average = 0;
int sum = 0;

Scanner keyboard = new Scanner(System.in);

System.out.println("Enter as many numbers as you wish. Enter -99 to finish your input.");



do {
int i = 0;
int numArray[];
numArray [i] = keyboard.nextInt();
sum = sum + numArray[i];
i++;
**} while (numArray[i] != -99);**

**average = sum/numArray.length;**

System.out.println("The sum of the numbers is " + sum + ".");
System.out.println("The average of the numbers is " + average + ".");

}

}

我在带星号的两行中收到错误。它说 numArray 不能解析为变量,i 也是如此。我使用 Eclipse 作为我的 IDE。

最佳答案

do {        
int i = 0;
int numArray[];
numArray [i] = keyboard.nextInt();
sum = sum + numArray[i];
i++;
} while (numArray[i] != -99);

numArray 是在 do while 循环的范围内定义的。所以你不能从范围之外访问它(即在这里你不能访问它,因为你试图在右大括号之后访问它)。

要解决这个问题,请在周围范围内定义 numArray

这同样适用于i

关于java - 我的java数组代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31613392/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com