-1
Hello, I’m new to the language, I’m an intern at a programming company and my boss asked me to perform a small program to exercise my learning, asked me to make a menu on the console in java.
-each item needs to have a description, example: entries -each item can have other items, such as people, products. -menu items are conditioned to be displayed only when the user is allowed to do so. -the challenge is to create the program so that it is possible to insert the menu structure and the program to assemble it according to the informed structure. -a structure is also required to indicate which items the user has access to.
I realized the menu so far, the first difficulty is that I choose an option and the program ends, I need it to continue working to select other options, I’m trying to save what the person type on the keyboard for an arraylist in case the registrations, the permission part I have no idea how to do, someone give me a light please kkkk
my code until then:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner menu = new Scanner (System.in);
System.out.print("##--Teste Estrutura de Menu--##\n\n");
System.out.print("|-----------------------------|\n");
System.out.print("| Opção 1 - Novo Cadastro |\n");
System.out.print("| Opção 2 - Clientes |\n");
System.out.print("| Opção 3 - Produtos |\n");
System.out.print("| Opção 4 - Sair |\n");
System.out.print("|-----------------------------|\n");
System.out.print("Digite uma opção: ");
int opcao = menu.nextInt();
switch (opcao) {
case 1:
System.out.print("\nOpção Novo Cadastro Selecionado");
break;
case 2:
System.out.print("\nOpção Clientes Selecionado\n");
break;
case 3:
System.out.print("\nOpção Produtos Selecionado\n");
break;
default:
System.out.print("\nOpção Inválida!");
break;
case 4:
System.out.print("\nAté logo!");
menu.close();
}
}
}
Then I found out I had to wear a loop, I think the best would be the do.. while in my case, however I am unable to implement in my code.
package programaJava;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner menu = new Scanner (System.in);
do {
System.out.print("##--Teste Estrutura de Menu--##\n\n");
System.out.print("|-----------------------------|\n");
System.out.print("| Opção 1 - Novo Cadastro |\n");
System.out.print("| Opção 2 - Clientes |\n");
System.out.print("| Opção 3 - Produtos |\n");
System.out.print("| Opção 4 - Sair |\n");
System.out.print("|-----------------------------|\n");
System.out.print("Digite uma opção: ");
int opcao = menu.nextInt();
if (opcao == 4) {
System.out.print("\nAté logo!");
menu.close();
}
switch (opcao) {
case 1:
System.out.print("\nOpção Novo Cadastro Selecionado");
break;
case 2:
System.out.print("\nOpção Clientes Selecionado\n");
break;
case 3:
System.out.print("\nOpção Produtos Selecionado\n");
break;
default:
System.out.print("\nOpção Inválida!");
break;
}
}
}
What do you want? https://answall.com/q/146875/101 Or this? https://answall.com/q/390450/101
– Maniero
menu.close()
closes theScanner
, which in turn closes theSystem.in
. But you should not close theSystem.in
, it is a "special" resource, managed by JVM and once closed, you can’t reopen it.– hkotsubo