Home > database >  Find minimum, maximum and average of random number in java
Find minimum, maximum and average of random number in java

Time:10-19

I need to randomize 1000 numbers, and I want to find the average of all the numbers, the smallest and the largest number, but I can't output it. Here is my code:

import java.util.Scanner;

public class Array1 {

public static void main(String[] args)
{ 
  Scanner keyboard=new Scanner(System.in); 
  int sum=0;
  int min;
  int max;
  double average;
  int number=0;
  int[] A = new int[1000]; 
  for(int i=0;i<A.length;i  )
  {
     A[i]=(int)(Math.random()*100);
     System.out.print(A[i] " ");
     if(((i 1) % 10) == 0){
        System.out.println();
     }
     sum=sum A[i];
     average = sum / A.length;
  
     min=A[0];
     if (A[i]<min){
        min=A[i];
     }
     max=A[0];
     if (A[i]>max){
        max=A[i];
     }
  }
  
 
  System.out.println("The average value is:"  average);
  System.out.println("The minimum value is:"  min);
  System.out.println("The maximum value is:"  max);


  }
  }

I don't know how to fix it

CodePudding user response:

import java.util.Scanner;

public class Array1 {

public static void main(String[] args)
{ 
  Scanner keyboard=new Scanner(System.in); 
  int sum=0;
  int min = 0;
  int max = 0;
  double average = 0;
  int number=0;
  int[] A = new int[1000];
  for(int i=0;i<A.length;i  )
  {
      A[i]=(int)(Math.random()*100); // generates 1000 rand ints initially
  }
  min=A[0];
  max=A[0];
  for(int i=0;i<A.length;i  )
  {
     System.out.print(A[i] " ");
     if(((i 1) % 10) == 0){
        System.out.println();
     }
     sum=sum A[i];  
     
     if (A[i]<min){
        min=A[i];
     }
     
     if (A[i]>max){
        max=A[i];
     }
  }
  average = sum / A.length;
  
 
  System.out.println("The average value is:"  average);
  System.out.println("The minimum value is:"  min);
  System.out.println("The maximum value is:"  max);


  }
  }

CodePudding user response:

Here is a link to look and checkout it should explain your current problem which is your Class Array is Public and you need to declare it.

-> https://learn.sourcegraph.com/how-to-troubleshoot-java-error-class-is-public-should-be-declared-in-its-own-file

  • Related