Kali ini saya postingkan program java pencarian nilai terbesar dan terkecil.
User akan menginputkan 5 data, kemudian program akan mencari data yang di inputkan user yang paling terbesar dan terkecil.
import java.io.*;
import java.lang.*;
public class MinMax{
public static void main(String [] args)throws IOException
{
BufferedReader baca=new BufferedReader(new InputStreamReader(System.in));
int i;
int maks=-1000; //asumsi paling minimum
int min=1000; //asumsi paling maksimum
int[] no=new int[5];
System.out.println("Masukan Nilai:");
for (i=0; i<5; i++)
{
System.out.print("Input Nilai Data ke- "+(i)+": ");
//System.out.println("Input Data");
no[i]=Integer.parseInt(baca.readLine());
}
for (i=0; i<5; i++)
{
System.out.println("data"+(i)+":"+no[i]);
}
for (i=0;i<5;i++)
{
if(no[i]>maks)
{
maks=no[i];
}
if(no[i]<min)
{
min=no[i];
}
System.out.println(" ");
}
System.out.println("nilai max"+maks);
System.out.println(" ");
System.out.println("nilai min"+min);
}
}
Labels: Java