i am having trouble with lets say i do not have any test scores and enter -1 there is an error of dividing by zero.

import java.util.Scanner;

public class myarray 
{  

     
	public static void main(String args[ ]) 
   {
	
		Scanner kbd = new Scanner(System.in);
	
	//total number of quiz scores possible in CS 200
		final int MAX_SIZE = 10;  

	//Create (declare & instantiate) an int array of size 10, called: quizScores
		int [] quizScores = new int[MAX_SIZE];

	//Declare any other variable storage space needed by the program
		int number;
		int sum =0;
		int ave =0;
		int index;
		int count =0;

	
	/*Initialize the first 7 values to your quiz scores thus far in this course from user keyboard input 
	with a "do-while loop", sentinel value (-1) to stop loop,
	and counter to keep track of how many values you have assigned to the array. */

	do
	{
		System.out.println("Please enter your quiz scores. Enter -1 to quit.");
		number = kbd.nextInt();
		quizScores[count] = number;
		
		if(quizScores[count] != -1)
		{
			

			count++;
		
		}
		}
		while(number != -1 && count < quizScores.length);
		

	/*Next, use the counter  from the loop above to run a 
	"for loop" to sum your quiz scores. */
	for(index = 0; index < count; index++)
		{
			sum = sum + quizScores[index];
		}
		
	/*Then, find the average score of your quizzes thus far 
	using the sum divided by the counter*/
			ave = sum / count;
	
	//Display (echo) your individual scores 
		for(index = 0; index < count; index++)
		{		
			System.out.print(quizScores[index] + " ");
		}
	//Display the sum of the scores
		System.out.println();
		System.out.println("The sum of the quiz scores is: " + sum);
	//Display the quiz score average 
	System.out.println("The average quiz score is: " + ave);				
  } //closing main method
} //closing class header

Recommended Answers

All 2 Replies

That's because you go ahead and compute the average, even if there are no numbers. The average calculation is sum/count so if count is zero you get a divide by zero error.
After the user has entered his numbers, but before you calculate the average, you can test for count == 0 and stop there (eg return; from your method).

this is the answer

import java.util.Scanner;

public class myarray 
{  

     
	public static void main(String args[ ]) 
   {
	
		Scanner kbd = new Scanner(System.in);
	
	//total number of quiz scores possible in CS 200
		final int MAX_SIZE = 10;  

	//Create (declare & instantiate) an int array of size 10, called: quizScores
		int [] quizScores = new int[MAX_SIZE];

	//Declare any other variable storage space needed by the program
		int number;
		int sum =0;
		int ave =0;
		int index;
		int count =0;
			
	/*Initialize the first 7 values to your quiz scores thus far in this course from user keyboard input 
	with a "do-while loop", sentinel value (-1) to stop loop,
	and counter to keep track of how many values you have assigned to the array. */

	do
	{
		System.out.println("Please enter your quiz scores. Enter -1 to quit.");
		number = kbd.nextInt();
		quizScores[count] = number;
		
		if(quizScores[count] != -1)
		{
			count++;
		}
		}
		while(number != -1 && count < quizScores.length);	
	
	/*Next, use the counter  from the loop above to run a 
	"for loop" to sum your quiz scores. */
	if(count > 1)
	{
	
		for(index = 0; index < count; index++)
		{
			sum = sum + quizScores[index];
		}
			if(number == -1)
			{
	
	/*Then, find the average score of your quizzes thus far 
	using the sum divided by the counter*/
			ave = sum / count;
			}
			else
			{
				ave = sum/count;
			}
		}
	
	//Display (echo) your individual scores 
		for(index = 0; index < count; index++)
		{			
			System.out.print(quizScores[index] + " ");
		}
	
	//Display the sum of the scores
		System.out.println();
		System.out.println("The sum of the quiz scores is: " + sum);
	
	//Display the quiz score average 
	System.out.println("The average quiz score is: " + ave);				
  
  } //closing main method
} //closing class header
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.