CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2016
    Posts
    1

    Question Need help with this EOF based and running total loop?

    Hi, here are the instructions for the project I have to do:
    http://pastebin.com/n7wyyDWA

    I'm new to java and I'm trying my best to figure this out on my own, but after about 6 hours of trying, my brain is a bit fried. I'd like to know what I'm doing wrong and what I need to add.
    Here is what I have so far:
    Code:
    import java.util.*;
    public class p5
    {
      static Scanner kb = new Scanner(System.in);
      public static void main (String[] args)    
      {
    // number entered and the running total
        int inputNum, sum = 0;
    // count of valid and invalid numbers seen
        int numValid = 0, numInvalid = 0;
        double average;
        
        System.out.println("Enter a positive value (Ctrl-D to quit): ");
        while ( kb.hasNext() )
        {
          inputNum = kb.nextInt();
          if ( inputNum < 0 )
            System.out.println("The number " + inputNum + " is invalid.");
          
          
          else
            
            System.out.println("Enter a positive value (Ctrl-D to quit): ");
        }                    
        // System.out.println("The sum of the valid numbers was " + sum + " and the average was " + average + ".");
      }
    }
    I know I need the valid and invalid numbers to add up and create a sum and average, but I have no idea how to do that.

    If anyone could shed some light, I'd really like to learn about what I'm doing wrong/right and not just be given the answer. I don't want to seem like I'm cheating, I just need some guidance.

    Thanks!

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Need help with this EOF based and running total loop?

    I'd like to know what I'm doing wrong
    Please explain why you think there is something wrong.
    Copy any output here and add some comments about what is wrong and show what you want the output to be.
    Norm

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured