// PositiveOdd3.java
//
// Outputs an indication of whether all
// the long integers in an array are
// both positive and odd.
//
// Uses console input
import javax.swing.JOptionPane;
public class PositiveOdd3
{
public static void main(String[] args)
{
final int MAX_LENGTH = 50;
long[] numbers = new long[MAX_LENGTH];
int lengthFilled = 0;
while ( true )
{
String numberText = JOptionPane.showInputDialog(
"Enter an integer (or X to quit)");
if ( numberText.charAt(0) == 'X'
|| numberText.charAt(0) == 'x' )
break;
numbers[lengthFilled++] = Long.parseLong(numberText);
}
System.out.print("The numbers ");
for ( int i = 0; i < lengthFilled; i++ )
System.out.print(" " + numbers[i]);
System.out.println(" "
+ (isPositiveOdd(numbers, lengthFilled)
?"ARE"
:"are NOT")
+ " all positive and odd." );
System.exit(0);
}
public static boolean isPositiveOdd(long[] array, int length)
{
for ( int i = 0; i < length; i++ )
if ( array[i] <= 0 || array[i] % 2 == 0 )
return false;
return true;
}
}