belajar java part 3 pertemuan 6

import java.util.Scanner;
class probabilitas
{
public int n,k;
Scanner input = new Scanner(System.in);
probabilitas()
{
menu();
}
public void inputData()
{
System.out.print(“MAsukkan nilai n:”);
n =input.nextInt();
System.out.print(“MAsukkan nilai k:”);
k =input.nextInt();
tanya();
}
public int fak(int n)
{
int jumlah = 1;
for(int i=1;i<=n;i++){
jumlah = jumlah*i;
}
return jumlah;
}
public void per()
{
if(n<k){
System.out.println("\nMath Error – n<k");
}
else{
System.out.println("\nP("+n+","+k+")"+"="+(fak(n)/fak(n-k)));
}
tanya();
}
public void kom()
{
if(n<k){
System.out.println("\nMath Error – n<k");
}
else{
System.out.println("\nC("+n+","+k+")"+"="+(fak(n)/(fak(n-k)*fak(k))));
tanya();
}
}
public void tanya()
{
System.out.print("\n\nKembali ke Menu?[Y/T]:");
String pilih = input.next();
if(pilih.equalsIgnoreCase("Y"))
{
menu();
}
else{
System.exit(0);
}
}
public void menu()
{
System.out.println("=============");
System.out.println("1. Input Data");
System.out.println("2. Permutasi ");
System.out.println("3. Kombinasi ");
System.out.println("4. Exit ");
System.out.print("Masukan pilihan anda:");
int pil=input.nextInt();
switch(pil){
case 1: inputData();break;
case 2: per();break;
case 3: kom();break;
case 4: System.exit(0);
default:menu();
}
}
public static void main(String[] args){
new probabilitas();
}
}

simpan dengan nama probabilitas.java

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s