WhatEver
WhatEver

Reputation: 27

Count numbers in a "for loop" and give back average, min, max value in java

A user should input a number "x", and then the count of "x" numbers, e.g. input = 3, then 3 random numbers like 3, 5, 7.

Afterwards the program should give an output of the average, min and max value of this "x" numbers. So it has to read the numbers, but i don't know how it can be done.

It should be done without arrays and with a for loop.

I didn't find a possible solution here, but maybe I didn't do the right search.

So here is what i got so far:

import java.util.Scanner;

public class Statistic
{
public static void main (String[] args)
 {
    // Variables
    Scanner input = new Scanner(System.in);
    int number1;
    int numbers;
    double averageValue;

    // Input
    System.out.println("\n\n####################################################################");
    System.out.print("\n Pls enter a number: ");
    number1 = input.nextInt();
    System.out.print(" Pls enter " + number1 +" numbers: ");
    numbers = input.nextInt();

    for (int count = 0; count < number1; count ++) {
        System.out.println(numbers); //Just for me to see which numbers are read by the programm
    }
    averageValue = numbers / number1;

    // Output
    System.out.println("\n Biggest number: " + Math.max(numbers));
    System.out.println("\n Smallest number: " + Math.min(numbers));
    System.out.print("\n Average value: " + averageValue);

 }
} 

But it only prints out and calculates with the first number of the "numbers"-input. Further I am not sure how to use the "Math.max" for a random count of numbers.

Upvotes: 0

Views: 1751

Answers (5)

Prasad Mule
Prasad Mule

Reputation: 1

import java.util.Scanner;

public class SumOf2 
{

    public static void main(String[] args) 
    {
        Scanner s = new Scanner(System.in);
        System.out.println("Enter 2 integer values");
        int m = s.nextInt();
        int n = s.nextInt();
        int sum=0;
        int count=0;
        for(int i=m ; i<=n ; i++)
        {
            if(count < n)
            {
                sum=sum+i;
                count++;
            }
        }
        System.out.println("Sum of two numbers is: "+sum);
        System.out.println("Count between 2 numbers is : "+count);
        

    }

}

Upvotes: 0

djm.im
djm.im

Reputation: 3323

Some errors that I can see

System.out.print(" Pls enter " + number1 +" numbers: ");
numbers = input.nextInt();

You need here a loop and array to read and store all elements.


To get average value you need first to sum all elements in array and then to divide by length of array.


To find min and max values in array you cannot use Math.min() and Math.max() methods because these methods get two parameters and return min/max value.


Your code should be something like this

Notes

  • If you cannot use Java 8 you must replace Arrays.stream(numbers).max().getAsInt(); and Arrays.stream(numbers).min().getAsInt(); with helper methods which find max/min values in an array.
  • If you can use Java 8 you can calculate sum int sum = Arrays.stream(numbers).reduce(0, (x, y) -> x + y); instead in for loop.

.

public class Statistic {
    public static void main(String[] args) {
        // Variables
        Scanner input = new Scanner(System.in);

        // Input
        System.out.println("\n\n####################################################################");
        System.out.print("\n Pls enter a number: ");

        int number1 = input.nextInt();
        System.out.println(" Pls enter " + number1 + " numbers: ");
        int[] numbers = new int[number1];
        for (int i = 0; i < number1; i++) {
            System.out.println("Enter next number");
            numbers[i] = input.nextInt();
        }

        // Find min and max values
        int max = Arrays.stream(numbers).max().getAsInt();
        int min = Arrays.stream(numbers).min().getAsInt();
        System.out.println("\n Biggest number: " + max);
        System.out.println("\n Smallest number: " + min);

        // Get average value
        int sum = 0;
        for (int num : numbers) {
            sum = sum + num;
        }
        double averageValue = (double) sum / number1;
        System.out.print("\n Average value: " + averageValue);

    }
}

Upvotes: 0

Kushagra Misra
Kushagra Misra

Reputation: 481

First your code is logically in correct. when u have to take min and max values with average u need to store the inserted elements or process each input(for time complexity this would be the best approach). Below I have modified your code where i m using enter code hereJava Collections List to store the inputs, sort them and get the data. After sorting first will me min and last will be max. Math.min and Math.max only works for comparing 2 numbers not an undefined list. Again i would say the best solution would be if u check for the number is min or max at input time. As you are new to java you can try that out your self.

import java.util.*;

public class ZahlenStatistik
{
    public static void main (String[] args)
    {
        // Variables
        Scanner input = new Scanner(System.in);
        int number1;
        List<Integer> numbers = new ArrayList<Integer>(); // change it to list
        double averageValue;
        int sum= 0;

        // Input
        System.out.println("\n\n####################################################################");
        System.out.print("\n Bitte geben Sie eine Zahl ein: ");
        number1 = input.nextInt();
        System.out.print(" Bitte geben Sie " + number1 +" Zahlen ein: ");

        //Define the number of times loop goes
        for (int count = 0; count < number1; count ++)
        {
            numbers.add(input.nextInt());
        }

        for(Integer number:numbers)
        {
            sum = sum + number;
        }

        averageValue = sum / number1;
        Collections.sort(numbers);
        // Output
        System.out.println("\n Die größte Zahl ist: " + numbers.get(numbers.size()-1));
        System.out.println("\n Die kleinste Zahl ist: " + numbers.get(0));
        System.out.print("\n Der averageValue betr\u00e4gt: " + averageValue);

    }
}

Upvotes: 0

Stefan Lazarević
Stefan Lazarević

Reputation: 37

You will need 6 variables: min = 0, max = 0, avg, sum = 0, count, num. (avg variable is optional)

Program flow will be:

  • input how many numbers you want to enter -> store in variable count
  • use some loop to loop count number of times and in each iteration store users value in variable num.
  • Increment sum by number user entered. sum += num;
  • check if entered number is less than current min. If true store min as that number.
  • Same as min do for max variable.
  • When loop exit you will have min, max, sum and count variables stored. To calculate avg devide sum with count and there you go. avg = sum / count.

Upvotes: 1

techfly
techfly

Reputation: 1866

The problem is here:

System.out.print(" Bitte geben Sie " + number1 +" Zahlen ein: ");
numbers = input.nextInt();

nextInt() only saves one int. Every subsequent number you are entering gets lost, of course.

What you need to do is to move this statement inside the for loop for your idea to work.

Also, you can't use min and max here. min and max compare two numbers and return the greater of the two. For your purpose, you'd need to check inside the loop which the greatest and smallest number is and then output it accordingly.

Upvotes: 1

Related Questions