开发者

Program Runs Continually, but never actually executes anything

I run the program, but none of my lines execute. When I tell it to st开发者_JAVA百科op it prints a red error message.

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)

Here's my code, nothing really seems to be out of the ordinary to my limited experience and my IDE doesn't report any errors while I'm writing it.

import java.util.Scanner;

public class 312easf2 
{

    public static void main(String[] args) 
    {
        Scanner keyboard = new Scanner(System.in);
        int grade = 0;  // initial value to satify loop condition
        double averageGrade = 0.0;
        int max = keyboard.nextInt();
        int min = max;
        int next = keyboard.nextInt();

        System.out.println("Enter a nonnegative integer (negative to stop): ");

        while(next >= 0);
        {
            if(next > max)
                max = next;
            else if(next < min);
                min = next;
            next = keyboard.nextInt();
        }
    }

}


EDIT: I point out a syntax gotcha in my answer, which you still have to fix before your program will work correctly, but for now the real problem lies in your first few lines:

        Scanner keyboard = new Scanner(System.in);
        int grade = 0;  // initial value to satify loop condition
        double averageGrade = 0.0;
        int max = keyboard.nextInt();
        int min = max;
        int next = keyboard.nextInt();

Your program is executing something but it's saying nothing, and printing the error only when you stop, all because of a mistake in your program's implementation. It's not a syntax error, however; it's just that you forgot something important here.

Wcrousse in his answer explains what's happening.


There is a syntax issue are multiple similar syntax issues somewhere in your code that are actually legal syntax, which is why your IDE and the compiler aren't complaining. However, they're more often really a very common mistake that causes unintended behavior.

Pay particular attention to your semicolons, and try looking through your code again. Or, use your debugger as Gabe suggests, that'll give you a better idea what's happening.


semicolon aside, I wonder if you realize that the program, as you have written it, requires the user to enter a number for max and another number for next before it prints the message, "Enter a nonnegative integer (negative to stop): ." It then takes another number for next and enters the loop. Because the prompt for a number is outside the loop, it requires the user to continue entering numbers without printing anything further.


I would recommend you to use some nice IDE with code analysis features. It will make your life easier and will help you to avoid such annoying mistakes wich can drive you nuts because you just made a stupid typo and can't figure out what is the problem.

Look at IntelliJ IDEA Community Edition (it's free) or at Eclipse (but I'm not sure about automatic code analisys in Eclipse, I don't use it).

P.S.

 while(next >= 0);

remove the semicolon and it should do something..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜