Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Java program to generate 10 random integers and then sum computed

  1. #1
    Junior Member
    Join Date
    May 2009
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Question Java program to generate 10 random integers and then sum computed

    So I'm trying to have the program generate 10 random integers and then have the sum computed. Please help.


    import java.util.Random;
    import java.util.*;

    public final class Sum10RandomIntegers
    {
    public static final void main(String[] args)
    {

    int sum;
    int randomInt;

    System.out.println("Generating 10 random integers in range 0 to 10.");

    Random randomGenerator = new Random();

    for (int random = 1; random <= 10; ++random)
    {
    randomInt = randomGenerator.nextInt(10);
    sum = (int)(Math.random() * 10);
    System.out.println(" " + sum);
    }

    }


    }
    Last edited by Deep_4; November 7th, 2012 at 10:44 PM.


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Re: What am I missing?

    Hello Lizard and welcome to the Java Programming Forums

    You were very close with this.. You just needed to do the calculation.

    import java.util.Random;
    import java.util.*;
     
    public final class Sum10RandomIntegers {
        public static final void main(String[] args) {
     
            int sum = 0;
            int randomInt;
            [B]int result = 0;[/B]
     
            System.out.println("Generating 10 random integers in range 0 to 10.");
     
            Random randomGenerator = new Random();
     
            for (int random = 1; random <= 10; ++random) {
                randomInt = randomGenerator.nextInt(10);
                sum = (int) (Math.random() * 10);
                System.out.print(sum + " ");
                [B]result = result+sum;[/B]
     
            }
            System.out.println();
            System.out.println("Result = " + result);
        }
     
    }

    Example output:

    Generating 10 random integers in range 0 to 10.
    8 5 3 5 3 7 8 6 3 5
    Result = 53
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

  3. The Following User Says Thank You to JavaPF For This Useful Post:

    Lizard (May 14th, 2009)

Similar Threads

  1. Java error in password generator program
    By Lizard in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 16th, 2009, 07:49 PM
  2. Replies: 2
    Last Post: March 6th, 2009, 03:00 PM