在使用tutorialspoint.com时出现了NoSuchElementException错误。

huangapple go评论73阅读模式
英文:

NoSuchElementException when using tutorialspoint.com

问题

以下是翻译好的内容:

我正在使用这个编译器网站 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++。

英文:

So I was working on my code using this compiler site: https://www.tutorialspoint.com/compile_java_online.php

And when I ran my code it have me this error:

Exception in thread &quot;main&quot; 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)

This is my code:

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(&quot;Enter a value: &quot;);
        value = input.nextInt();
       
        while (value != 0){
            if (value &lt; 0){
                negative++;
            }
            else {
                positive++;
            }
           
            sum += value;
           
            System.out.print(&quot;\nEnter a value: &quot;);
            value = input.nextInt();
           
        }
         System.out.print(
            &quot;Positive integers: &quot; + positive +
                &quot;\nNegative integers: &quot; + negative +
                &quot;\nSum: &quot; + sum +
                &quot;\nAverage: &quot; + ((sum * 1.0) /(positive/negative)) 
            );
       
           
    }
}

It prints out enter a value to accept the value but doesn't calculate or do anything. I'm not sure what part of my code is messing up. Im also kinda new to Java and I'm used to using C++.

答案1

得分: 6

你需要在 STDIN 标签中提供输入。例如:

1
-1
0

这是特定于你正在使用的 tutorialspoint 网站的问题,因为它不提供交互式控制台。如果你在本地运行代码,将不会出现该错误。

英文:

You need to provide input on the STDIN tab. For example:

1
-1
0

This is a problem specific to the tutorialspoint site you are using because it does not provide an interactive console. If you run the code locally, you will not get that error.

答案2

得分: 2

我从未使用过这个网络编译器,但如果我理解正确的话,在运行代码之前,您将不得不在“STDIN”选项卡中指定输入,然后它将逐行获取输入。

由于您的代码只有在输入“0”时才会结束,因此“STDIN”中的最后一行必须是零。例如,我尝试了以下一组数字的代码:

10
1
-3
02
2300
0

这将产生以下输出:

输入一个值:
输入一个值:
输入一个值:
输入一个值:
输入一个值:
输入一个值:正整数:4
负整数:1
总和:2310
平均值:577.5

附加信息

我不知道对于刚开始学习Java的人是否推荐使用这样的在线编译器,因为我对它们的可靠性不确定,但我更建议使用诸如Eclipse或IntelliJ IDEA之类的集成开发环境。在这种情况下,IDE实际上允许您在代码运行时输入数字,而无需预先定义它们。

英文:

I've never used this web compiler, but if I understand it correctly you'll have to specify the input (in the STDIN tab) before running the code and it will grab the input line by line.

As your code only end's if "0" is supplied the last line in STDIN has to be a zero. For example I tried the code with the following set of numbers:

10
1
-3
02
2300
0

Which gives the following output:

Enter a value: 
Enter a value: 
Enter a value: 
Enter a value: 
Enter a value: 
Enter a value: Positive integers: 4
Negative integers: 1
Sum: 2310
Average: 577.5

Additional info:

I don't know if it is recommendable for someone new to java to use such online compilers as I'm not sure about their reliability, but I'd rather recommend using an IDE such Eclipse or IntelliJ IDEA. In this case the IDE would actually allow you to input the numbers while the code is running instead of having to have it predefined.

huangapple
  • 本文由 发表于 2020年9月14日 01:45:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/63873808.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定