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:

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


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

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