所以我正在使用这个编译器站点编写我的代码:https://www.tutorialspoint.com/compile_java_online.php
当我运行我的代码时,出现以下错误:
Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at WorkingWithLoops.main(WorkingWithLoops.java:22)
这是我的代码:
import java.util.Scanner;
public class WorkingWithLoops {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int negative = 0, positive = 0, sum = 0, value;
System.out.print("Enter a value: ");
value = input.nextInt();
while (value != 0){
if (value < 0){
negative++;
}
else {
positive++;
}
sum += value;
System.out.print("\nEnter a value: ");
value = input.nextInt();
}
System.out.print(
"Positive integers: " + positive +
"\nNegative integers: " + negative +
"\nSum: " + sum +
"\nAverage: " + ((sum * 1.0) /(positive/negative))
);
}
}
它打印出输入一个值以接受该值,但不计算或执行任何操作。我不确定我的代码的哪一部分出了问题。我对 Java 也有点陌生,我习惯使用 C++。
您需要在 STDIN 选项卡上提供输入。例如:
1
-1
0
这是您正在使用的tutorialspoint 站点特有的问题,因为它不提供交互式控制台。如果您在本地运行代码,则不会出现该错误。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)