Java
* Using a do-while loop to process a menu selection
*/
import java.util.Scanner;
public class JavaHelp
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
char choice;
do
{
System.out.println("Help on Syntax : ");
System.out.println("1. while");
System.out.println("2. do-while");
System.out.println("3. for");
System.out.println("4. QUIT");
System.out.println("Choose any one : ");
choice = scan.next().charAt(0);
}while(choice < '1' || choice > '3');
System.out.println("\n");
switch(choice)
{
case '1' :
System.out.println("The while :\n");
System.out.println("while(condition)\n{");
System.out.println("\t// body of loop\n}");
break;
case '2' :
System.out.println("The do-while :\n");
System.out.println("do\n{");
System.out.println("\t// body of loop\n\n}while(condition);");
break;
case '3' :
System.out.println("The for :\n");
System.out.println("for(initialization; condition; iteration)\n{");
System.out.println("\t// body of loop\n}");
break;
case '4' :
System.exit(0);
break;
default :
System.out.println("Invalid Choice");
}
}
}
Output :
When the above JAVA program is compiled or executed, it will produce the following results,
1.
2.
3.
Menubased Program in JAVA : Syntax Help
30 June
0
Write a program (WAP) in JAVA to Menu-based selection for know the syntax of While, do...while and for loop.
/* Java Program - Java do-while Loop* Using a do-while loop to process a menu selection
*/
import java.util.Scanner;
public class JavaHelp
{
public static void main(String args[])
{
Scanner scan = new Scanner(System.in);
char choice;
do
{
System.out.println("Help on Syntax : ");
System.out.println("1. while");
System.out.println("2. do-while");
System.out.println("3. for");
System.out.println("4. QUIT");
System.out.println("Choose any one : ");
choice = scan.next().charAt(0);
}while(choice < '1' || choice > '3');
System.out.println("\n");
switch(choice)
{
case '1' :
System.out.println("The while :\n");
System.out.println("while(condition)\n{");
System.out.println("\t// body of loop\n}");
break;
case '2' :
System.out.println("The do-while :\n");
System.out.println("do\n{");
System.out.println("\t// body of loop\n\n}while(condition);");
break;
case '3' :
System.out.println("The for :\n");
System.out.println("for(initialization; condition; iteration)\n{");
System.out.println("\t// body of loop\n}");
break;
case '4' :
System.exit(0);
break;
default :
System.out.println("Invalid Choice");
}
}
}
Output :
When the above JAVA program is compiled or executed, it will produce the following results,
1.
2.
3.
Must Read :-
Previous article
Next article
Leave Comments
Post a Comment