使用tutorialspoint.com时出现NoSuchElementException [英] NoSuchElementException when using tutorialspoint.com

查看:84
本文介绍了使用tutorialspoint.com时出现NoSuchElementException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我正在使用此编译器网站来处理代码: https://www.tutorialspoint.com /compile_java_online.php

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 "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 ++.

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++.

推荐答案

您需要在STDIN选项卡上提供输入.例如:

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

1
-1
0

这是您正在使用的tutorialspoint站点的特定问题,因为它不提供交互式控制台.如果您在本地运行代码,则不会收到该错误.

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.

这篇关于使用tutorialspoint.com时出现NoSuchElementException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆