Kin*_*ing 0 java java.util.scanner
所以我正在使用这个编译器站点处理我的代码: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)
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
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))
);
}
}
Run Code Online (Sandbox Code Playgroud)
它打印出输入一个值以接受该值,但不计算或执行任何操作。我不确定我的代码的哪一部分搞砸了。我对 Java 也有点陌生,我习惯于使用 C++。
您需要在 STDIN 选项卡上提供输入。例如:
1
-1
0
Run Code Online (Sandbox Code Playgroud)
这是您使用的tutorialspoint 站点特有的问题,因为它不提供交互式控制台。如果您在本地运行代码,则不会出现该错误。
| 归档时间: |
|
| 查看次数: |
81 次 |
| 最近记录: |