3 Sample Coding JAVA Program

3 Sample Coding JAVA Program
As a result of the training material before the material, the following is an example of a simple program example that uses all statements, there is one program help menu, 2 courses seek to know the storage directory and third guessing game program numbers.


Sample JAVA program Making the help menu:


01 class HelpMenu {
02 public static void main(String args[]) throws java.io.IOException {
char choice;
03 for(;;) {
04 do {
05 System.out.println(“Help Menu For Syntax:”);
System.out.println(” 1. if”);
06 System.out.println(” 2. switch”);
07 System.out.println(” 3. for”);
08 System.out.println(” 4. while”);
09 System.out.println(” 5. do-while”);
10 System.out.println(” 6. break”);
11 System.out.println(” 7. continuen”);
System.out.print(“Choose one (q for exit): “);
12 do {
13 choice = (char) System.in.read();
14 } while(choice == ‘n’ | choice == ‘r’);
15 } while( choice < ‘1’ | choice > ‘7’ & choice != ‘q’); if(choice == ‘q’) break;
16 System.out.println(“n”); switch(choice) {
17 case ‘1’:
18 System.out.println(“format if:n”);
System.out.println(“if(condition) statement;”);
19 System.out.println(“else statement;”);
20 break;   
21 case ‘2’:
22 System.out.println(“format switch:n”);
System.out.println(“switch(expression) {“);
23 System.out.println(” case constant:”);
System.out.println(” statement sequence”);
24 System.out.println(” break;”);
25 System.out.println(” // …”);
26 System.out.println(“}”);
27 break;
28 case ‘3’:
29 System.out.println(“format for:n”);
System.out.print(“for(init; condition; iteration)”);
30 System.out.println(” statement;”);
31 break;   
32 case ‘4’:
33 System.out.println(“format while:n”);
System.out.println(“while(condition) statement;”);
34 break;
35 case ‘5’:
36 System.out.println(“format do-while:n”);
System.out.println(“do {“);
System.out.println(” statement;”);
37 System.out.println(“} while (condition);”); break;
38 case ‘6’:
39 System.out.println(“format break:n”);
System.out.println(“break; or break label;”); break;
40 case ‘7’:
41 System.out.println(“format continue:n”);
System.out.println(“continue; or continue label;”);
42 break;
43 }
44 System.out.println();
45 }
46 }
47 }


JAVA program Making the help menu
coding results, the above example is a java program to select the help menu to see the syntax of the statement that there is





Examples of java program for Displaying Directory Storage Folder program:


01 import java.io.*;
02 public class FindFolder{
03 public static void main(String[] args){
04 if (args.length == 0)
05 args = new String[] { “..” };
06 try{
07 File pathName = new File(args[0]);
String[] fileNames = pathName.list();
08 for (int i = 0; i < fileNames.length; i++){
09 File f = new File(pathName.getPath(), fileNames[i]); if (f.isDirectory()){
10 System.out.println(f.getCanonicalPath());
main(new String[] { f.getPath() });
11 }
12 }
13 }
14 catch (IOException e){
15 e.printStackTrace();
16 }
17 }
18 }


java program for Displaying Directory Storage Folder program
coding results, will feature in the directory where the program that we created is saved

Examples of java program Guess Game Score:

01 import java.io.*; import java.util.*;
02 public class GuessNumber {
03 public static void main(String args[]){ Scanner scan=new Scanner(System.in); BufferedReader reader;
04 Random rand=new Random();
05 System.out.print(“Input limit value of <min max>: “); intmin=scan.nextInt();
06 int max=scan.nextInt(); int acakAngka=0; while(acakAngka<min)
07 acakAngka=rand.nextInt(max);     int tebak=-1;
08 int jumTebak=0;
09 reader=new BufferedReader(new InputStreamReader(System.in));
10 System.out.println(“Guess the number between “+min+” to “+max+” !”); boolean invalid=false;
11 do{
12 jumTebak++; try{
13 System.out.print(“Your guess: “); String line=reader.readLine();
14 if((line==null) || line.equals(“quit”)) break; tebak=Integer.parseInt(line);
15 }
16 catch(IOException ioe){} catch(NumberFormatException nfe){
17 System.out.println(“Invalid value (not integer)!”); tebak=-1;
18 invalid=true;
19 }
20 if (tebak>=min && tebak <=max){ if (tebak==acakAngka)
21 System.out.println(“Wow, your guess is right The Number : “+jumTebak+” GOOD JOB!”);
22 else if(tebak<acakAngka)
23 System.out.println(“Guess the number is even greater!”);
24 else
25 System.out.println(“Guess the number is even smaller!”);
26 }
27 else if(invalid){
28 System.out.println(“Remember the number between “+min+” to “+max+”!”);
29 }
30 }while(tebak!=acakAngka);
31 }
32 }
java program Guess Game Score
the results of coding, example programs inputted guess the numbers between min and max numbers randomly

Post Author: Fajar (Ucup)

Pemilik dan Author dari FajarYusuf.com dan juga seseorang yang gemar membagikan info menarik kepada orang lain.