
Basic java help =)
Hi,
I'm brand new to Java. I have taken a couple classes of c++ before but just started this Java one.
I have an assignment to create a program that generates three random numbers and then calculates the average for them.
I thought I had it finished pretty quick and easy, until I realized its not averaging quite right. If the average is 13.66666, it'll just output 13.0. if its 7.43 it'll output 7.0. Always .0.
How do I go about getting it to output the correct decimal, and how do I set it to for example, two decimal slots?
This is what I have for my program so far.
Code:
package chapter3ec;
import java.util.Random;
public class RandomNumbers
{
public static void main( String [ ] args )
{
Random random = new Random( );
// Generate three random numbers between 0 and 50
int start = 0, end = 50;
System.out.println( "Generating three numbers between " + start
+ " and " + end + "...");
int number = random.nextInt( end  start + 1 ) + start;
int number2 = random.nextInt( end  start + 1 ) + start;
int number3 = random.nextInt( end  start + 1 ) + start;
//Outputs each of the three numbers generated.
System.out.println( "First number: " + number);
System.out.println( "Second number: " + number2);
System.out.println( "Third number: " + number3);
//Calculates the average of the three numbers
float average = (number + number2 + number3) / 3;
//Outputs the average of the three numbers
System.out.println( "The average of the three numbers is: " + average);
}
}
Edit: I think I just realized its not the output that is the issue, but that its probably doing integer math and not storing the decimal into average at all. Trying to figure it out now, I probably can since I know what I'm looking for now.
Edit2: I figured it out.
I changed
float average = (number + number2 + number3) / 3;
to:
float average = (float) ((number + number2 + number3) / 3.0) ;
Nevermind guys =). Thanks anyways though. I may be back as I have a couple other assignments to do today as well.

int number = random.nextInt(50);