Ask Experts Questions for FREE Help !
Ask
    haha123456's Avatar
    haha123456 Posts: 1, Reputation: 1
    New Member
     
    #1

    Oct 16, 2013, 04:01 PM
    Finding the average of random numbers a user inputs
    Here's my code. How do I get the output average when the user enters the random numbers. I keep getting 0.0 for the average.
    Package averages;
    import java.util.Scanner;
    /**
    *
    * @author Kramer1
    */
    Public class Averages {

    /**
    * @param args the command line arguments
    */
    Public static void main(String[] args) {
    // TODO code application logic here
    Scanner in = new Scanner(System.in);

    Double sum = 0;
    int count = 0;
    Double input = 0;
    System.out.println("Enter in as many numbers as you want and the average will be calculated: ");
    Do{

    System.out.print ("Please enter a number and when finished type a -1 to stop: ");
    input = in.nextDouble();

    }
    While (input != -1);
    Count++;

    if (input != -1){
    Sum = (input + sum);
    Double average = sum / count;

    System.out.println("Average of the numbers is " + average);
    }else{
    System.out.println("No Data.");
    }



    }
    }
    Celine91's Avatar
    Celine91 Posts: 51, Reputation: 9
    Junior Member
     
    #2

    Oct 17, 2013, 09:55 AM
    This can be the c++ version of your code, it successfully calculates the average of random numbers the user inputs and terminates for -1 * as specified in your code*
    ---------------------------------------------------------------------
    #include <iostream>
    using namespace std;
    int main()
    {
    int input;
    double sum = 0;
    int count = 0;

    cout<<"Enter the numbers to which you want their average calculated:"<<endl;
    cout<<"Enter -1 in order to terminate input! "<<endl;
    do
    {
    cin>>input;
    if (input == -1) break;
    sum= sum + input;

    count ++;
    }while (input > 0);

    cout<<" The average of the numbers is: "<<sum/count <<endl;


    return 0;
    }
    -------------------------------------------------------

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Writing a program about random numbers [ 0 Answers ]

1. Write a program that randomly generates a number between 50 and 100. Based off the number that is randomly generated, do the following: (25 points) If the randomly generated number is 93 or greater, display: “Great job! You got an A!” If 85-92, display: “Not too shabby, we’ll take a B!” If...

Random Numbers? [ 5 Answers ]

This has to be a difficult problem to solve so any input is greatly appreciated. Quick Overview: I play this online game (racing game) and the point of the game is to "guess" as close as possible to the "magic number". I am trying to figure out how to predict the next number in the "random"...

Random numbers , variable arrays [ 1 Answers ]

here is my problem To create a program that generates 7 random numbers display it in 7 labels (not repeat same number twice)and there a button if u click it picks out the highest number. Im a beginner so I need code.

Generating random numbers [ 11 Answers ]

Not gernating same number twice in a row

Interactive C++ program that inputs a series of 12 temperatures from the user. [ 2 Answers ]

Interactive C++ program that inputs a series of 12 temperatures from the user.


View more questions Search