abonini
abonini

Reputation: 33

How to do an addition from random numbers by the user?

I am new to Java and I would like some help. I have to solve this problem and I have it almost 90% solved:

  1. Prompt the user to enter number of students. It must be a number that is perfectly divisible by 10 i.e. (number % 10) = 0
  2. Check user input. If user input is not divisible by 10, keep asking the user for input until he enter a right number.
  3. Accept user input and generate that many random numbers in the range from 0 to 100.
  4. Print a matrix of random numbers and calculate the sum and average of all these random numbers and print them to the user.
  5. Format sum and average to three decimal points.

This is my code so far:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Calculator10 {

    public static void main(String[] args) {

        Scanner user_input = new Scanner(System.in);

        int num;

        do {
            System.out.print("Enter a number: ");
            num = user_input.nextInt();
        } while(num % 10 != 0);

        double numb;
        DecimalFormat dec = new DecimalFormat("0.00");
        for (int i=0; i<num; i++){
            numb = Math.abs(Math.random() * ( 0 - 100 ));
            System.out.print(" " +dec.format(numb) + " ");
        }
    }
}

As you can see, I have solved until the first part of # 4. I am not sure how I could sum all those random numbers displayed on the screen after user input. Of course, we have to store them in an array but I tried to do that but couldn't. So, how could I complete step #4 and 5? I would appreciate any help. Thanks a lot guys.

Upvotes: 0

Views: 1007

Answers (2)

Chandranshu
Chandranshu

Reputation: 3669

Here is how you should do it:

import java.text.DecimalFormat;
import java.util.Scanner;

public class Calculator10 {

    public static void main(String[] args) {

        Scanner user_input = new Scanner(System.in);

        int num;

        do {
            System.out.print("Enter a number: ");
            num = user_input.nextInt();
        } while(num % 10 != 0);

        double numb;
        double sum=0;
        DecimalFormat dec = new DecimalFormat("0.00");
        for (int i=0; i<num; i++){
            numb = Math.random() * ( 100 - 0));
            System.out.print(" " + dec.format(numb) + " ");
            sum += numb;
        }

        System.out.println("The sum is: " + dec.format(sum));
        System.out.println("The average is:" + dec.format(sum/num));
    }
}

Please note that I have slightly changed the way you were generating the random numbers which obviates the need to use Math.abs(). Also see the following answer to see how to generate random numbers between two different values:

Upvotes: 2

Gavin Haynes
Gavin Haynes

Reputation: 1992

You do not need to store them in an array. Just declare int sum = 0 at the start and do sum += numb each time you generate a random number. Also, you are generating random numbers in a strange way. Take a look at the java.util.Random class.

Upvotes: 0

Related Questions