I write a simple average program, but for some reason I keep getting compiler error. I tried to check my work, but still can't find where do I do wrong. It shows the message on average = total/10, and it says something like unable to reach statement.

//test
import java.util.Scanner;
public class increment

{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		int total=0;	
		int grade;
		int average;
		int counter=0;
		
		
		while (0<10)
		{
		System.out.println("Please enter the nubmer: ");
		grade = input.nextInt();
		total = total + grade;
		counter++;
		}		
		average = total/10;
		System.out.println("The average is "+ average);
		
		
	
	}

}

Recommended Answers

All 3 Replies

In your while loop, you have while(0 < 10). O will always be less than 10 so it would never come out of the while loop.You need:

while(counter < 10)

You may want to use doubles for total, grade, and average. Integer division will drop off the decimal(i.e. Integer Division: 5/2 = 2 Double Division: 5.0/2.0 = 2.5)
If you want to use doubles,

import java.util.Scanner;
public class increment
 
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		double total=0;	
		double grade;
		double average;
		int counter=0;
 
 
		while (0<10)
		{
		System.out.println("Please enter the nubmer: ");
		grade = input.nextDouble();
		total = total + grade;
		counter++;
		}		
		average = total/10;
		System.out.println("The average is "+ average);
 
 
 
	}
 
}

But you may not need to use doubles for this program.

Member Avatar for ztini

By naming convention, class names should be in mixed case starting with uppper case.

public class Increment

And yes, this class should use doubles instead of integers. You're taking the average of 10 numbers -- so unless you assume that the modulus of the sum of those numbers will always equal 0, you need a double to catch the decimal point.

Oh, Okay. Thank you guys so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.