Hi guys I wait you're nice I have a question about arrays in Java, I wanna add a sentence with if and else where the array have a rank for example:
String [] menu = {"Arroz","Papas","Pollo","Sopa","carne","Bandeja Paisa"};
System.out.println("Bienvenido al restaurante x ¿Desea probar el menu del dia o alguna comida especifica?");
String usuario = input.nextLine();
if ((usuario.equals("Menu")||(usuario.equals("Menu del dia"))))
{for(int i = 0; i < menu.length; i++){System.out.println("Los platos para el dia de hoy son: "+ menu[i]);}
System.out.println("¿Cual deseas comer?");String opcion = input.nextLine();
instead of use a cicle I want print the arrays with a certain rank in a only message.
also I wanna do a switch with possibles input of strings because I think that is the better way to use in this situation instead of a "if" but this don't work,for example
{System.out.println("Los platos para el dia de hoy son: " + Arrays.toString(menu));}
System.out.println("¿Cual deseas comer?");String opcion = input.nextLine();
switch(){
case 1: opcion.equals("Pollo");
System.out.println("El plato " + opcion + " estara listo dentro de 60 segundos.");
break;
Sorry if you don't understand, English isn't my native language and I deny use translator or similar. Thanks very much.
You could write out the menus with the Java-8 forEach method:
System.out.println("Los platos para el dia de hoy son:");
java.util.Arrays.asList(menu).forEach(System.out::println);
Your switch statement could read like this (you will need at least Java 1.7):
switch(opcion) {
case "Pollo":
System.out.println("El plato " + opcion + " estara listo dentro de 60 segundos.");
break;
Make these changes:
Like this:
if ((usuario.equals("Menu")||(usuario.equals("Menu del dia")))) {
System.out.println("Los platos para el dia de hoy son:);
for (int i = 0; i < menu.length; i++) {
System.out.println(i + ": " + menu[i]);
}
System.out.println("¿Cual deseas comer?");
int opcion = Integer.parseInt(input.nextLine());
String plato = menu[opcion];
}
You should consider using a Map<String, Integer>
to hold the plate prices. If you do that, with the above code, you don't need a switch
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.