开发者

Why is my code not printing anything to stdout? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

I'm trying to calculate the average of a student's marks:

import java.util.Scann开发者_Go百科er;

public class Average

{

    public static void main(String[] args)
    {
        int mark;
        int countTotal = 0;  // to count the number of entered marks
        int avg = 0;        // to calculate the total average
        Scanner Scan = new Scanner(System.in);

        System.out.print("Enter your marks: ");
        String Name = Scan.next();

        while (Scan.hasNextInt())
        {
            mark = Scan.nextInt();
            countTotal++;

            avg = avg + ((mark - avg) / countTotal);
        }


        System.out.print( Name + "  " + avg );
    } 
}


Here's a solution that uses two Scanner (as suggested in my previous answer).

  • Scanner stdin = new Scanner(System.in); scans user's input
  • Scanner scores = new Scanner(stdin.nextLine()); scans the line containing the scores

Note also that it uses a much simpler and more readable formula for computing the average.

        Scanner stdin = new Scanner(System.in);

        System.out.print("Enter your average: ");
        String name = stdin.next();

        int count = 0;
        int sum = 0;
        Scanner scores = new Scanner(stdin.nextLine());
        while (scores.hasNextInt()) {
            sum += scores.nextInt();
            count++;
        }
        double avg = 1D * sum / count;
        System.out.print(name + "  " + avg);

Sample output:

Enter your average: Joe 1 2 3
Joe  2.0


Because it's still sitting in the while loop, waiting for an instruction to break out of the loop.

Here's a Sun tutorial on the subject. The course materials you got should also contain this information by the way.


In my previous post (previous answer) I indicated that you need to press Ctrl+D after you are done inputting numbers. Ctrl+D signals to your program that there is no more input.

Try entering:

WM 1 2 3 6

Then press Ctrl+D


You should use getline and accept an "exit" string. String.split and some of the Integer.valueOf stuff makes parsing numbers from string literals written in very easy in Java.


How about this?

import java.util.Scanner;

public class Average{

public static void main(String[] args)
{
    int mark = 0;
    int countTotal = 0;  // to count the number of entered marks
    int avg = 0;        // to calculate the total average
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter student name: ");
    String name = scan.next();

    System.out.println("Please enter marks one by one?");


    while(scan.hasNextInt()){

        int tempMark = scan.nextInt();
        mark += tempMark;
        countTotal+=1;

        System.out.println("If you are done with mark, type \"Done!\"");
    }

    avg = mark/countTotal;

    System.out.print( name + "  " + avg );
} 

}

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜