Need this to end when a negative number is entered
I'm hung up here on how to get this to end when a negative number is entered. I want the user to enter 10 integers into an array. After those are entered, the program will sum and average the data. Inputs and outputs use JOptionPane. When a negative number is entered, the program should end. That's where I'm stuck. The while statement is what I can't seem to figure out. Any pointers? Have I given enough information for what I am asking?
Code :
import java.text.NumberFormat;
import java.text.DecimalFormat;
import javax.swing.*;
public class Array
{
public static void main(String args[])
{
int checkNumber =0;
int inputArray[] = new int[10];
int arrayLength = inputArray.length;
Boolean keepRunning = new Boolean (true);
for(int i=0;i<arrayLength;i++)
checkNumber = Integer.parseInt(JOptionPane.showInputDialog("Enter integer for array location "+i+" :"));
while (checkNumber>=0)
{
int sum = 0;
String loc = "";
for(int i=0; i<arrayLength; i++)
{
sum+= inputArray[i];
loc+= "Array location " + i + " is " + inputArray[i] + "\n";
}
double avg;
NumberFormat formatter = new DecimalFormat ("#0.000");
avg =(double)sum/10;
JOptionPane.showMessageDialog (null, loc + "The sum or the array is " + sum + "\n The average of the array is " + formatter.format(avg));
}
}
}
Re: Need this to end when a negative number is entered
Code :
if ( checkNumber < 0 ){
return;
}