Script dibawah ini adalah program Menghitung Nilai Mahasiswa dengan menggunakan bahasa Pemrograman Java. Programnya saya bikin 2 dengan menggunakan For dan Array. Lihat Scriptnya dan perhatikan perbedaannya.
Dengan Menggunakan For
Dengan Menggunakan For
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int jum=0;
double rata;
garis();
System.out.println(" MENU PILIHAN");
garis();
System.out.println("1. MENGHITUNG NILAI MAHASISWA");
System.out.println("2. EXIT");
System.out.println("");
garis();
System.out.print("ANDA PILIH NOMOR :");
int pil=input.nextInt();
switch(pil){
case 1 :{
garis();
System.out.println("MENGHITUNG NILAI MAHASISWA");
garis();
System.out.print("MASUKAN JUMLAH MAHASISWA : ");
int j=input.nextInt();
for(int i=1;i<=j;i++)
{
System.out.print(i+". ");
for(int n=1; n<=1; n++)
{
System.out.print("NPM\t : ");
String npm=input.next();
System.out.print(" NAMA : ");
String nama=input.next();
System.out.print(" JURUSAN : ");
String jurusan=input.next();
System.out.print(" MATAKULIAH : ");
String matakuliah=input.next();
System.out.print(" NILAI TUGAS : ");
int tugas=input.nextInt();
System.out.print(" NILAI UTS : ");
int uts=input.nextInt();
System.out.print(" NILAI UAS : ");
int uas=input.nextInt();
jum=(tugas+uts+uas)/3;
System.out.println(" NILAI RATA : "+jum);
}
System.out.println(" ");
}
break;
}
case 2:{
System.exit(0);
break;
}
default:System.exit(0);
}
}
static void garis (){
System.out.println("===============================");
}
}
public class Menu {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int jum=0;
double rata;
garis();
System.out.println(" MENU PILIHAN");
garis();
System.out.println("1. MENGHITUNG NILAI MAHASISWA");
System.out.println("2. EXIT");
System.out.println("");
garis();
System.out.print("ANDA PILIH NOMOR :");
int pil=input.nextInt();
switch(pil){
case 1 :{
garis();
System.out.println("MENGHITUNG NILAI MAHASISWA");
garis();
System.out.print("MASUKAN JUMLAH MAHASISWA : ");
int j=input.nextInt();
for(int i=1;i<=j;i++)
{
System.out.print(i+". ");
for(int n=1; n<=1; n++)
{
System.out.print("NPM\t : ");
String npm=input.next();
System.out.print(" NAMA : ");
String nama=input.next();
System.out.print(" JURUSAN : ");
String jurusan=input.next();
System.out.print(" MATAKULIAH : ");
String matakuliah=input.next();
System.out.print(" NILAI TUGAS : ");
int tugas=input.nextInt();
System.out.print(" NILAI UTS : ");
int uts=input.nextInt();
System.out.print(" NILAI UAS : ");
int uas=input.nextInt();
jum=(tugas+uts+uas)/3;
System.out.println(" NILAI RATA : "+jum);
}
System.out.println(" ");
}
break;
}
case 2:{
System.exit(0);
break;
}
default:System.exit(0);
}
}
static void garis (){
System.out.println("===============================");
}
}
Dengan Menggunakan Array
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int i, jum=0;
String[] NPM=new String[15];
String[] NAMA=new String[25];
String[] JUR=new String[25];
String[] MK=new String[25];
int[] TUGAS=new int[5];
int[] UAS=new int[5];
int[] UTS=new int[5];
double rata;
garis();
System.out.println(" MENU PILIHAN");
garis();
System.out.println("1. MENGHITUNG NILAI MAHASISWA");
System.out.println("2. EXIT");
System.out.println("");
garis();
System.out.print("ANDA PILIH NOMOR : ");
int pil=input.nextInt();
switch(pil){
case 1 :{
garis();
System.out.println("MENGHITUNG NILAI MAHASISWA (ARRAY)");
garis();
System.out.print("MASUKAN JUMLAH MAHASISWA : ");
int j=input.nextInt();
for(i=1; i<=j; i++)
{
System.out.print(i+". ");
System.out.print("NPM\t : ");
NPM[i]=input.next();
System.out.print(" NAMA : ");
NAMA[i]=input.next();
System.out.print(" JURUSAN : ");
JUR[i]=input.next();
System.out.print(" MATAKULIAH : ");
MK[i]=input.next();
System.out.print(" NILAI TUGAS : ");
TUGAS[i]=input.nextInt();
System.out.print(" NILAI UTS : ");
UTS[i]=input.nextInt();
System.out.print(" NILAI UAS : ");
UAS[i]=input.nextInt();
jum=TUGAS[i]+UTS[i]+UAS[i];
rata=jum/3;
System.out.println(" NILAI RATA : "+rata);
System.out.println(" ");
}
break;
}
case 2:{
System.exit(0);
break;
}
default:System.exit(0);
}
}
static void garis (){
System.out.println("===============================");
}
}
public class Menu {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
int i, jum=0;
String[] NPM=new String[15];
String[] NAMA=new String[25];
String[] JUR=new String[25];
String[] MK=new String[25];
int[] TUGAS=new int[5];
int[] UAS=new int[5];
int[] UTS=new int[5];
double rata;
garis();
System.out.println(" MENU PILIHAN");
garis();
System.out.println("1. MENGHITUNG NILAI MAHASISWA");
System.out.println("2. EXIT");
System.out.println("");
garis();
System.out.print("ANDA PILIH NOMOR : ");
int pil=input.nextInt();
switch(pil){
case 1 :{
garis();
System.out.println("MENGHITUNG NILAI MAHASISWA (ARRAY)");
garis();
System.out.print("MASUKAN JUMLAH MAHASISWA : ");
int j=input.nextInt();
for(i=1; i<=j; i++)
{
System.out.print(i+". ");
System.out.print("NPM\t : ");
NPM[i]=input.next();
System.out.print(" NAMA : ");
NAMA[i]=input.next();
System.out.print(" JURUSAN : ");
JUR[i]=input.next();
System.out.print(" MATAKULIAH : ");
MK[i]=input.next();
System.out.print(" NILAI TUGAS : ");
TUGAS[i]=input.nextInt();
System.out.print(" NILAI UTS : ");
UTS[i]=input.nextInt();
System.out.print(" NILAI UAS : ");
UAS[i]=input.nextInt();
jum=TUGAS[i]+UTS[i]+UAS[i];
rata=jum/3;
System.out.println(" NILAI RATA : "+rata);
System.out.println(" ");
}
break;
}
case 2:{
System.exit(0);
break;
}
default:System.exit(0);
}
}
static void garis (){
System.out.println("===============================");
}
}
Outputnya akan tampil seperti dibawah ini.
Catatan :
Ketika anda mengcompile programnya. Kemudian anda menginputkan NPM sampai NILAI UAS. Saya sarankan jangan menggunakan spasi. Kalau Anda penasaran apa yang akan terjadi, silakan dicoba menggunakan spasi.