Input a list of positive numbers, terminated by 0, into an array Numbers[]. Then display the array and the largest and smallest number in it.
Input a list of positive numbers, terminated by 0, into an array Numbers[].
Then display the array and the largest and smallest number in it.
The solution is designed in a simpler way.
import java.util.Arrays;
import java.util.Scanner;
public class ReadingWithScanner {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int [] myArray = new int[5];
int smallest = myArray[0];
int largest = myArray[0];
System.out.println("Enter the elements of the array:");
for(int i=0; i<myArray.length; i++ )
{
int l =s.nextInt();
if (l != 0)
{
myArray[i] = l;
}
else{
System.out.println("exit");
break;
}
}
System.out.println(Arrays.toString(myArray));
Arrays.sort(myArray);
System.out.println(Arrays.toString(myArray));
for(int sm=0; sm<5; sm++)
{
if(myArray[sm] >0)
{
System.out.println("Smallest is "+myArray[sm]);
break;
}
else{
System.out.println("x");
}
}
System.out.println("Biggest is "+myArray[myArray.length-1]);
}
}
Comments
Post a Comment