英文:
My arraylist size is a zero even though I've added items to the it
问题
所以我尝试查找了这个错误(```Exception in thread "main" java.lang.ArithmeticException: / by zero```)
然后发现我在下面的代码中,我的arraylist大小为零。我无法理解为什么它是零,或者如何解决它。这与数组的容量有关吗?我似乎无法弄清楚我的代码有什么问题。
顺便说一下,这段代码是用于计算arraylist中所有元素的平均值,并让用户知道平均值是多少。
我在Java方面还是一个初学者,所以如果这看起来很愚蠢,我很抱歉。任何帮助都将不胜感激!
import java.util.*;
public class ClassStuff {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Integer> myArray = new ArrayList<Integer>();
int userInput = 0;
String userConf = "";
while ((!userConf.equalsIgnoreCase("y"))) {
System.out.println("Please enter a number: ");
userInput = scan.nextInt();
for (int i = 1; i <= myArray.size(); i++) {
userInput = scan.nextInt();
myArray.add(userInput);
}
scan.nextLine();
System.out.println("Are you done entering numbers? (y/n): ");
userConf = scan.nextLine();
}
int result = computeAvg(myArray);
System.out.println("Average is: " + result);
}
public static int computeAvg(List<Integer> myArray) {
int sum = 0;
int avg = 0;
for (int i = 0; i < myArray.size(); i++) {
sum = sum + myArray.get(i);
}
return avg = sum/myArray.size();
}
}
<details>
<summary>英文:</summary>
So I tried looking up this error (```Exception in thread "main" java.lang.ArithmeticException: / by zero```)
and figured that my arraylist size in the code below is zero. I can't understand why it is zero or how to solve it. Does it have to do something with the capacity of the array? I can't seem to figure out what's wrong with my code.
Btw, this code is for computing the average of all the elements in an arraylist and letting the user know what the average is.
I'm still a beginner at Java so I apologize if this may seem silly. Any help is appreciated!
import java.util.*;
public class ClassStuff {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList <Integer> myArray = new ArrayList <Integer> ();
int userInput = 0;
String userConf = "";
while ((!userConf.equalsIgnoreCase("y"))) {
System.out.println("Please enter a number: ");
userInput = scan.nextInt();
for (int i = 1; i <= myArray.size(); i++) {
userInput = scan.nextInt();
myArray.add(userInput);
}
scan.nextLine();
System.out.println("Are you done entering numbers? (y/n): ");
userConf = scan.nextLine();
}
int result = computeAvg(myArray);
System.out.println("Average is: " + result);
}
public static int computeAvg(List <Integer> myArray) {
int sum = 0;
int avg = 0;
for (int i = 0; i < myArray.size(); i++) {
sum = sum + myArray.get(i);
}
return avg = sum/myArray.size();
}
}
</details>
# 答案1
**得分**: 1
我假设以下代码行:
```java
System.out.println("Please enter a number:");
userInput = scan.nextInt();
获取了您想要添加到ArrayList的元素数量,稍后我们通过for循环将其添加到列表中。在这种情况下,将其保留在另一个名为list_length
的变量中,因为userInput
在for循环中不断变化。
System.out.println("Please enter a number:");
list_length = scan.nextInt();
然后在此输入之后,将for循环更改为类似以下内容:
for(int i = 1; i <= list_length; i++) {
userInput = scan.nextInt();
myArray.add(userInput);
}
这是因为您将for循环的结束更改为myArray.size()
,但请记住它已经是0,因此for循环会在1 >= 0
时结束。您可能想要做的是将list_length
个数的数字添加到ArrayList中。
英文:
I'm assuming the lines:
System.out.println("Please enter a number: ");
userInput = scan.nextInt();
gets the amount of elements you want to add to the arraylist, which we later add to the list through the for loop. In that case keep it in another variable called list_length
, since userInput
constantly changes in the for loop.
System.out.println("Please enter a number: ");
list_length = scan.nextInt();
Then change the for loop after this input to something like:
for(int i = 1; i <= list_length; i++) {
userInput = scan.nextInt();
myArray.add(userInput);
}
This is because you changed the end of the for loop to myArray.size()
, but remember that it was already 0, so the for loop ends since 1 >= 0
. What you probably wanted to do was to add list_length
amount of numbers into the arraylist
答案2
得分: 0
我找到了你的问题。
问题出在你的for循环上,因为在循环期间arrayList未能捕获任何元素。
我还对方法进行了一些调整,以便计算平均值正确。
以下是一个示例
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList<Integer> myArray = new ArrayList<>();
int userInput = 0;
String userConf = "";
while ((!userConf.equalsIgnoreCase("y"))) {
System.out.println("请输入一个数字:");
userInput = scan.nextInt();
myArray.add(userInput);
scan.nextLine();
System.out.println("是否完成输入数字? (y/n): ");
userConf = scan.nextLine();
}
System.out.println(myArray);
double result = computeAvg(myArray);
System.out.println("平均值为:" + result);
}
public static double computeAvg(List<Integer> myArray) {
double sum = 0;
double avg = 0;
for (int i = 0; i < myArray.size(); i++) {
sum = sum + myArray.get(i);
}
avg = sum / myArray.size();
return avg;
}
输出
myList = [4,5]
average = (4 + 5) / 2 (myArray.size())= 4.5
希望对你有所帮助!
英文:
I found your problem.
The problem was your for loop because somehow the arrayList didn't catch any of the elements during the loop.
I also made some adjustment for the method so it counts the average correct.
Here's an example
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
ArrayList <Integer> myArray = new ArrayList<>();
int userInput = 0;
String userConf = "";
while ((!userConf.equalsIgnoreCase("y"))) {
System.out.println("Please enter a number: ");
userInput = scan.nextInt();
myArray.add(userInput);
scan.nextLine();
System.out.println("Are you done entering numbers? (y/n): ");
userConf = scan.nextLine();
}
System.out.println(myArray);
double result = computeAvg(myArray);
System.out.println("Average is: " + result);
}
public static double computeAvg(List <Integer> myArray) {
double sum = 0;
double avg = 0;
for (int i = 0; i < myArray.size(); i++) {
sum = sum + myArray.get(i);
}
avg = sum / myArray.size();
return avg;
}
Output
myList = [4,5]
average = (4 + 5) / 2 (myArray.size())= 4.5
Hope it was useful!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论