Sabtu, 25 Mei 2013

CONTOH – CONTOH PROGRAM ARRAY DALAM BAHASA JAVA


CONTOH – CONTOH PROGRAM ARRAY DALAM BAHASA JAVA

1.MENCETAK 2,3,4 MENURUN
public class array1
{
public static void main (String []args)
{
int i[]={2,4,3,5};
int x;
for (x=0;x<=3;x++)
{
System.out.println(i[x]);
}
}
}
2. MENCETAK NAMA-NAMA HARI
public class array2
{
public static void main (String []args)
{
String []hari={"senin","selasa","rabu","kamis","jumat","sabtu","minggu"};
int x;
for (x=0;x<=6;x++)
{
System.out.println(hari[x]);
}
}
}
3. MENDEKLARASIKAN ANGKA MENJADI HURUF
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Scanner;
public class array5
{
public static void main(String[]args)
{
Scanner masuk=new Scanner(System.in);
BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in));
int p;
String []nim=new String[10];
String []nama=new String[10];
String []nh=new String[10];
int []nilai=new int[10];
int n;
System.out.print("Berapa banyak data:");
n=masuk.nextInt();
for(p=0;p<n;p++)
{
System.out.print("input nim ke-"+(p+1)+": ");
try
{
nim [p]=dataIn.readLine();
}
catch(IOException e)
{
System.out.println("error!");
}
System.out.print("input nama ke-"+(p+1)+": ");
try
{
nama [p]=dataIn.readLine();
}
catch(IOException e)
{
System.out.println("error!");
}
System.out.print("input nilai ke-"+(p+1)+": ");
nilai [p]=masuk.nextInt();
if(nilai[p]<=80)
nh[p]="A";
else if(nilai[p]>=70)
nh[p]="B";
else if(nilai[p]>=60)
nh[p]="C";
else if(nilai[p]>=50)
nh[p]="D";
else
nh[p]="E";
}
System.out.println("--------------------------");
for(p=0;p<n;p++)
{
System.out.print(nim[p]+"|");
System.out.print("\t"+nama[p]+"|");
System.out.print("\t"+nilai[p]+"|");
System.out.print("\t"+nh[p]+"|");
System.out.print("\n");
}
System.out.print("----------------------------");
}
}
4. MENJUMLAHKAN NILAI MAHASISWA
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Scanner;
public class arraytugas
{
public static void main(String[]args)
{
Scanner masuk=new Scanner(System.in);
BufferedReader dataIn=new BufferedReader(new InputStreamReader(System.in));
int p;
String []nim=new String[10];
String []nama=new String[10];
String []alamat=new String[10];
String []nh=new String[10];
int[]nilaiuts = new int[10];
int[]nilaiuas = new int[10];
int[]nilaitotal = new int[10];
int []semester=new int[10];
int []nilaitgs=new int[10];
int n;
System.out.print("Berapa banyak data:");
n=masuk.nextInt();
for(p=0;p<n;p++)
{
System.out.print("input nim ke-"+(p+1)+": ");
try
{
nim [p]=dataIn.readLine();
}
catch(IOException e)
{
System.out.println("error!");
}
System.out.print("input nama ke-"+(p+1)+": ");
try
{
nama [p]=dataIn.readLine();
}
catch(IOException e)
{
System.out.println("error!");
}
System.out.print("input alamat ke-"+(p+1)+": ");
try
{
alamat [p]=dataIn.readLine();
}
catch(IOException e)
{
System.out.println("error!");
}
System.out.print("input semester ke-"+(p+1)+": ");
{
semester[p]=masuk.nextInt();
}
System.out.print("input nilai uas ke-"+(p+1)+": ");
nilaiuas [p]=masuk.nextInt();
System.out.print("input nilai uts ke-"+(p+1)+": ");
nilaiuts [p]=masuk.nextInt();
System.out.print("input nilai tgs ke-"+(p+1)+": ");
nilaitgs [p]=masuk.nextInt();
nilaitotal[p]=((nilaitgs[p]+nilaiuts[p]+nilaiuas[p])/3);
if(nilaitotal[p]<=80)
nh[p]="A";
else if(nilaitotal[p]>=70)
nh[p]="B";
else if(nilaitotal[p]>=60)
nh[p]="C";
else if(nilaitotal[p]>=50)
nh[p]="D";
else
nh[p]="E";
}
System.out.print("\n\n");
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
System.out.println("| nim | nama | alamat | semester | nilaitgs | nilaiuas | nilaiuts | nilai total| nilaihuruf|");
for(p=0;p<n;p++)
{
System.out.print("|"+"    "+nim[p]+"|");
System.out.print("     "+nama[p]+"|");
System.out.print("       "+alamat[p]+"|");
System.out.print("         "+semester[p]+"|");
System.out.print("         "+nilaitgs[p]+"|");
System.out.print("         "+nilaiuas[p]+"|");
System.out.print("         "+nilaiuts[p]+"|");
System.out.print("           "+nilaitotal[p]+"|");
System.out.print("          "+nh[p]+"|");
System.out.print("\n");
}
System.out.println("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++");
}
}

Tidak ada komentar:

Posting Komentar