If you are interested in knowing how to create a menu of alternatives with the java console, then stay and read this post where we will show you how to do it easily and quickly.
With this little guide you can learn to create your own projects. You can create this Java menu, this method will help you not to generate junk codes And best of all, you will create this menu from scratch. That is why it is essential to download, update and install Java to the latest version. It is also essential that you note that these lines of code must be listed in sequence, including blank lines.
How to make a menu of alternatives with Do while in Java
The repetitive Do While statement is extremely important to implement various tools. Among the most common uses is to make a menu, video game, perform recursive calculations, among many other uses.
What's more, This statement is very similar to the While. But in this circumstance, Do While executes the loop at the beginning and then evaluates the condition. This makes it mandatory for the loop to run at least once.
All the steps that we will show you below are facts in a main. The first thing you will need is a boolean, a numeric variable and a scanner.
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main (String[] arguments) {
- Scanner sn = new scanner (System.in);
- boolean output = false;
- int option; // We will save the user's option
- make{
- }
- While (! leave);
- }
- }
After this command chain, you must make me ask for options, and this procedure is done through Do While, you can do it with a While.
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main (String[] arguments) {
- Scanner sn = new scanner (System.in);
- boolean output = false;
- int option; // We will save the user's option
- make{
- }
- While (! leave);
- }
- }
Thanks to this, you won't be able to get out unless you put it true. Now you can place the alternatives you want. It is recommended that you use System.out.println and with this format. With three options it would be like this:
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main (String[] arguments) {
- Scanner sn = new scanner (System.in);
- boolean output = false;
- int option; // We will save the user's option
- make{
- System.out.println (“1. Option 1”);
- System.out.println (“2. Option 2”);
- System.out.println (“3. Option 3”);
- System.out.println (“4. Leave”);
- System.out.println (“Write one of the alternatives”);
- option = sn.nextInt ();
- }
- While (! leave);
- }
- }
Time to hit a switch to get the tasks you need done. In each case you must put the code you need, and in the last you must put exit = true and thus indicate that you intend to exit. What's more should set a default value and thus avoid checking that the number is correctly placed in the range of alternatives:
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main (String[] arguments) {
- Scanner sn = new scanner (System.in);
- boolean output = false;
- int option; // We will save the user's option
- make{
- System.out.println (“1. Option 1”);
- System.out.println (“2. Option 2”);
- System.out.println (“3. Option 3”);
- System.out.println (“4. Leave”);
- System.out.println (“Write one of the alternatives”);
- option = sn.nextInt ();
- change (option) {
- case 1:
- System.out.println (“You have selected option 1”);
- break;
- case 2:
- System.out.println (“You have selected option 2”);
- break;
- case 3:
- System.out.println (“You have selected option 3”);
- break;
- case 4:
- output = true;
- break;
- default:
- System.out.println (“Only numbers between 1 and 4”);
- }
- }
- While (! leave);
- }
- }
Learn how to use the alternatives menu with Make while in Java
To use the Make while menu, should enter the respective digit to the option you want to select and hit Enter. Thus, your option will be processed and executed. In case of entering a number outside the range of alternatives, the program will show you an alert about the available range.
The Do While menu is very easy to use and implement. Despite this, two considerations should be paid attention:
Reassign the output variable: You should never do without an exit option on your menu. Opposite case, users will be forced to select an option without being able to exit. Similarly, this option should reassign the value of the output variable to break the Do While loop.
Correctly locate the keys: In case your menu consists of many more structured sentences; you must maintain a correct margin with the dependencies. Opposite case, difficult to determine which key belongs to the Do While loop.
How to display multiple options within a menu in Java?
The visualization of alternatives runs when entering the Do While loop. Thus, we can show the user what they can do and then execute the option they choose.
This implementation is done sequentially. So, all the alternatives will be shown to you to the user at the same time.
The code to indicate the menu alternatives is as follows:
- System.out.println (“1. Option 1”);
- System.out.println (“2. Option 2”);
- System.out.println (“3. Option 3”);
- System.out.println (“4. Leave”);
What if an error appears?
It is possible that you have put a string instead of a number and that is the reason for the error, so for this you must put this special exception.
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class JavaApplication219 {
- public static void main (String[] arguments) {
- Scanner sn = new scanner (System.in);
- boolean output = false;
- int option; // We will save the user's option
- make{
- System.out.println (“1. Option 1”);
- System.out.println (“2. Option 2”);
- System.out.println (“3. Option 3”);
- System.out.println (“4. Leave”);
- try {
- System.out.println (“Write one of the alternatives”);
- option = sn.nextInt ();
- change (option) {
- case 1:
- System.out.println (“You have selected option 1”);
- break;
- case 2:
- System.out.println (“You have selected option 2”);
- break;
- case 3:
- System.out.println (“You have selected option 3”);
- break;
- case 4:
- output = true;
- break;
- default:
- System.out.println (“Only numbers between 1 and 4”);
- }
- } capture (InputMismatchException e) {
- System.out.println (“You must insert a number”);
- sn.next ();
- }
- }
- While (! leave);
- }
- }
This exception is InputMismatchException, which will be launched when, If that's the case, input cannot be converted to a number. You must put sn.next () so you don't enter an infinite loop if you are using Scanner. And with these codes your menu will be completed.
Related Post:
- How to hide the Windows taskbar 10 in a few steps
- How long should I allow my new tablet to charge? First charge
- Among us: find out how to get all pets on cell phone and pc
- The best Google extensions to see the weather or the weather on your mobile or PC
- Snapchat filters in Zoom, Discord y Skype: Use them easily
- How to change the resolution of a PDF online and make it sharper