gpt4 book ai didi

程序运行后出现Java错误

转载 作者:行者123 更新时间:2023-11-30 08:54:05 24 4
gpt4 key购买 nike

我的程序做了我想让它做的所有事情,但在它运行完成后,我收到了弹出的错误消息。查看我的代码,我找不到错误发生的位置。

Exception in thread "main" java.lang.IllegalStateException: Scanner closed
at java.util.Scanner.ensureOpen(Scanner.java:1070)
at java.util.Scanner.next(Scanner.java:1465)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at simplestatistics.SimpleStatistics.main(SimpleStatistics.java:102)

输入 -> Java 结果:1 包简单统计;

import java.util.*;
import java.lang.*;
import java.util.InputMismatchException;

public class SimpleStatistics {

public static double[] getUserInput(Scanner sc) {

List<Double> inputList = new ArrayList<Double>();

System.out.println("Please enter how many numbers you will be inputing");
int numberOfInputs = sc.nextInt();

for (int i = 0; i < numberOfInputs; i++) {
System.out.println("Please enter a number");
double userInput = sc.nextDouble();
inputList.add(userInput);
}
sc.close();

double[] arr = new double[inputList.size()];
for (int i = 0; i < arr.length; i++) {
arr[i] = inputList.get(i);
}
return arr;
}

public static double arithmeticMean(double[] nums) {

double mean = 0;
double sum = 0;

for (int i = 0; i < nums.length; i++) {
sum = sum + nums[i];
}
mean = sum / nums.length;

return mean;
}

public static double geometricMean(double[] nums) {

double gm = 1.0;
for (int i = 0; i < nums.length; i++) {
gm *= nums[i];
}
gm = Math.pow(gm, 1.0 / (double) nums.length);
return gm;
}

public static double[] minAndmax(double[] nums) {

double min = nums[0];
double max = nums[0];

for (int i = 1; i < nums.length; i++) {
if (nums[i] < min) {
min = nums[i];
} else if (nums[i] > max) {
max = nums[i];
} else {

}
}

double[] minAndmax = {min, max};
return minAndmax;
}

public static double[] scaleUp(double[] nums, int factor) {

for (int i = 0; i < nums.length; i++) {
nums[i] *= factor;
}
return nums;
}

public static double[] scaleDown(double[] nums, int factor) {

for (int i = 0; i < nums.length; i++) {
nums[i] /= factor;
}
return nums;
}

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);

double[] input = {1, 2.8, 5.3, 100, -5, -6.5};

System.out.println("Choose a option 1-6");

boolean exit = false;
while (!exit) {
System.out.println();
System.out.println("1) Arithmetic mean, 2) Geometric mean, 3) minAndmax, 4) Scale Up, 5) Scale Down, 6) Quit");
System.out.print("Input -> ");
int choice = sc.nextInt();
System.out.println();

switch (choice) {
case 1: {
// Arithmetic mean
System.out.println("Arithmetic mean");
System.out.println(arithmeticMean(getUserInput(sc)));
break;
}
case 2: {
// Geometric mean
System.out.println("Geometric mean");
System.out.println(arithmeticMean(getUserInput(sc)));
break;
}
case 3: {
// Min and max
System.out.println("Min and Max");
for (double i : minAndmax(getUserInput(sc))) {
System.out.print(i + ", ");
}
break;
}
case 4: {
// Scale Up
System.out.println("Scale Up");
System.out.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleUp(input, factor)) {
System.out.print(i + ", ");
}
break;
}

case 5: {
// Scale Down
System.out.println("Scale Down");
System.out.print("Please enter factor by which you want to scale -> ");
int factor = sc.nextInt();
for (double i : scaleDown(input, factor)) {
System.out.print(i + ", ");
}
break;
}

case 6: {
exit = true;
break;
}

}
}
}

这是我的代码链接

https://gist.github.com/Chunky1022/0775fba6692456ae1c8c#file-gistfile1-txt

最佳答案

在第一次调用 getUserInput 时,您关闭了扫描器。

public static double[] getUserInput(Scanner sc) {

...

sc.close();

...
}

只需删除 sc.close 行即可。

如果您真的想关闭它,请在您确定不再使用此扫描仪时将其关闭。但是,我不明白您为什么要关闭 System.in

关于程序运行后出现Java错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29551656/

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