J
judith
My programs working now except for one problem when the program asks
you to enter a number and you enter anything other than a number it
outputs the error (Error , please enter the number again) but if it's
on Enter number 1 and i enter a letter like t when it goes back to
Enter a number it counts backward instead of asking to Enter number 1
again and i don't know what to do. I tried adding an i++ statement and
that didn't work. can anyone please help Judith
C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
3
Enter number 2
t
Error, please enter the number again.
Enter number 1
t
Error, please enter the number again.
Enter number 0
3
Enter number 1
3
Enter number 2
3
Enter number 3
3
The average is 5.0
C:\>
// Author: Judith Spurlock
// Course: ITSE 2417
// Program No: 3
// Due Date: 10/20/2006
//
// Program Name: program3JS.java
import java.util.Scanner;
import java.util.InputMismatchException;
public class program3JS
{
public static void main (String[] args)
{
// Variable declarations
boolean error = true;
double average;
int sum = 0;
int n = 0;
int i = 0;
int num = 0;
Scanner keyboard = new Scanner(System.in);
// Loop until there is no error
do
{
try
{ error = false;
System.out.println("How many numbers do you want to enter?");
n = keyboard.nextInt();
if (n <= 0 )
throw new Exception ("Number must be greater than 0.");
}
catch (Exception e)
{
String message = e.getMessage();
System.out.println(message);
error = true;
}
}
while (error);
// Loop through each number and calculate the average
for ( i = 0; i < n; i ++)
{
// Repeat input as long as there is an error
do
{
try
{
error = false;
System.out.println("Enter number " + (i+1));
num = keyboard.nextInt();
sum += num;
}
catch(InputMismatchException e)
{
keyboard.nextLine();
System.out.println("Error, please enter the number again.");
error = true;
i ++;
}
}
while (error);
}
average = sum/n;
System.out.println ("The average is " + average);
}
}
you to enter a number and you enter anything other than a number it
outputs the error (Error , please enter the number again) but if it's
on Enter number 1 and i enter a letter like t when it goes back to
Enter a number it counts backward instead of asking to Enter number 1
again and i don't know what to do. I tried adding an i++ statement and
that didn't work. can anyone please help Judith
C:\>java program3JS
How many numbers do you want to enter?
3
Enter number 1
3
Enter number 2
t
Error, please enter the number again.
Enter number 1
t
Error, please enter the number again.
Enter number 0
3
Enter number 1
3
Enter number 2
3
Enter number 3
3
The average is 5.0
C:\>
// Author: Judith Spurlock
// Course: ITSE 2417
// Program No: 3
// Due Date: 10/20/2006
//
// Program Name: program3JS.java
import java.util.Scanner;
import java.util.InputMismatchException;
public class program3JS
{
public static void main (String[] args)
{
// Variable declarations
boolean error = true;
double average;
int sum = 0;
int n = 0;
int i = 0;
int num = 0;
Scanner keyboard = new Scanner(System.in);
// Loop until there is no error
do
{
try
{ error = false;
System.out.println("How many numbers do you want to enter?");
n = keyboard.nextInt();
if (n <= 0 )
throw new Exception ("Number must be greater than 0.");
}
catch (Exception e)
{
String message = e.getMessage();
System.out.println(message);
error = true;
}
}
while (error);
// Loop through each number and calculate the average
for ( i = 0; i < n; i ++)
{
// Repeat input as long as there is an error
do
{
try
{
error = false;
System.out.println("Enter number " + (i+1));
num = keyboard.nextInt();
sum += num;
}
catch(InputMismatchException e)
{
keyboard.nextLine();
System.out.println("Error, please enter the number again.");
error = true;
i ++;
}
}
while (error);
}
average = sum/n;
System.out.println ("The average is " + average);
}
}