3 Contoh Coding Program JAVA

3 Contoh Coding Program JAVA

Sebagai hasil dari latihan materi materi sebelumnya , berikut adalah contoh contoh program sederhana yang memakai semua statement , ada 1 program help menu , 2 program mencari tau direktori penyimpanan dan 3 program permainan tebak angka.
Langsung saja kita kecontoh programnya.

Contoh program JAVA Membuat menu bantuan :



class HelpMenu {
public static void main(String args[]) throws java.io.IOException { char choice;
for(;;) {
do {
System.out.println("Help Menu For Syntax:"); System.out.println(" 1. if");
System.out.println(" 2. switch");
System.out.println(" 3. for");
System.out.println(" 4. while");
System.out.println(" 5. do-while");
System.out.println(" 6. break");
System.out.println(" 7. continue\n"); System.out.print("Choose one (q for exit): "); 
do {
choice = (char) System.in.read();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '7' & choice != 'q'); if(choice == 'q') break;
System.out.println("\n"); switch(choice) {
case '1':
System.out.println("format if:\n"); System.out.println("if(condition) statement;"); 
System.out.println("else statement;");
break;    
case '2':
System.out.println("format switch:\n"); System.out.println("switch(expression) {"); 
System.out.println(" case constant:"); System.out.println(" statement sequence"); 
System.out.println(" break;"); 
System.out.println(" // ...");
System.out.println("}"); 
break;
case '3':
System.out.println("format for:\n"); System.out.print("for(init; condition; iteration)"); 
System.out.println(" statement;");
break;    
case '4':
System.out.println("format while:\n"); System.out.println("while(condition) statement;"); 
break;
case '5':
System.out.println("format do-while:\n"); System.out.println("do {"); System.out.println(" statement;"); 
System.out.println("} while (condition);"); break;
case '6':
System.out.println("format break:\n"); System.out.println("break; or break label;"); break;
case '7':
System.out.println("format continue:\n"); System.out.println("continue; or continue label;"); 
break;
}
System.out.println();
}
}
}

Contoh program JAVA Membuat menu-menu bantuan
hasil coding , contoh diatas adalah program java untuk memilih menu bantuan untuk melihat sintaks dari statement yang ada



Contoh program java untuk Menampilkan Direktori Folder Penyimpanan program:



import java.io.*;
public class FindFolder{
public static void main(String[] args){
if (args.length == 0)
args = new String[] { ".." };
try{
File pathName = new File(args[0]); String[] fileNames = pathName.list();
for (int i = 0; i < fileNames.length; i++){
File f = new File(pathName.getPath(), fileNames[i]); if (f.isDirectory()){
System.out.println(f.getCanonicalPath()); main(new String[] { f.getPath() });
}
}
}
catch (IOException e){
e.printStackTrace();
}
}
}

program java untuk Menampilkan Direktori Folder Penyimpanan program
hasil coding , akan menampilkan pada direktori mana program yang kita buat disimpan

Contoh program java Permainan Tebak Angka :


import java.io.*; import java.util.*;
public class GuessNumber {
public static void main(String args[]){ Scanner scan=new Scanner(System.in); BufferedReader reader;
Random rand=new Random();
System.out.print("Input limit value of <min max>: "); int min=scan.nextInt();
int max=scan.nextInt(); int acakAngka=0; while(acakAngka<min)
acakAngka=rand.nextInt(max);     int tebak=-1;
int jumTebak=0;
reader=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Guess the number between "+min+" to "+max+" !"); boolean invalid=false;
do{
jumTebak++; try{
System.out.print("Your guess: "); String line=reader.readLine();
if((line==null) || line.equals("quit")) break; tebak=Integer.parseInt(line);
}
catch(IOException ioe){} catch(NumberFormatException nfe){
System.out.println("Invalid value (not integer)!"); tebak=-1;
invalid=true;
}
if (tebak>=min && tebak <=max){ if (tebak==acakAngka)
System.out.println("Wow, your guess is right The Number : "+jumTebak+" GOOD JOB!");
else if(tebak<acakAngka)
System.out.println("Guess the number is even greater!");
else
System.out.println("Guess the number is even smaller!");
}
else if(invalid){
System.out.println("Remember the number between "+min+" to "+max+"!");
}
}while(tebak!=acakAngka);
}
}
program java Permainan Tebak Angka :
hasil coding, contoh program tebak angka yang diinput antara angka min dan max secara random
Artikel Terkait:
Disqus Comments