5
Contributors
6
Replies
7
Views
7 Years
Discussion Span
Last Post by peter_budo
-1

how do u add the elements?

this is my program

import java.util.Scanner;

public class RandomNumbers
{
  public static void main(String [] args)
  {
	Scanner scan= new Scanner(System.in);
	
	int pos, value;
		
	System.out.println("Enter value of numbers to generate(0-100): ");
	value=scan.nextInt();
	
	while (value<0 || value>100)
	{
	System.out.println("Please enter value again (0-100): ");
	}	
		double[]random =new double[value];
				
		for (pos=0; pos< value; pos++)
		{
		random[pos]=Math.random()*1000.0 + 17.5;
		}
				
		System.out.println("Here are the random numbers: ");
		for (pos=0; pos<value; pos++)
		System.out.println(random[pos]);	
		
	}
}

i need to generate the sum of the random numbers.

0

//pseudocode
for (lowest array value to highest array value)
{
add array value to total sum
}

-1
double Total = 0;
        int i; 
	for (i=0; i< 10; i++)
	{
		double rdNumber = Math.random() *1000.0 + 17.5;
		Total+=rdNumber;
	}
	System.out.println("Sum: " +Total );

Sample loop to accumulate total value.

Edited by truebot: n/a

1
  1. Global declaration of for loop variable "pos" will for sure create numerous issues in more complex application, so local declaration inside the for loop is better to use
  2. You missed the point of this forum, people should learn by trying things rather then be given solution for copying
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.