Para mi clase de Java, tenemos que detectar la entrada del usuario de una habilidad de Pokémon y dar una salida de la descripción de dicha habilidad usando un interruptor. Tengo el escáner para detectar una entrada, pero cuando escribo algo que no sea el número de caso, simplemente me da un error. No sé cómo hacer que detecte una habilidad como 'Clorofila' y luego hacer que use la salida a la consola.
Aquí está mi código:
package pokemonAbility; import java.util.Scanner; public class PokemonAbility { public static void main(String[] args) { System.out.println("Enter Pokemon ability:"); Scanner scanner = new Scanner(System.in); int abilityPkmn = scanner.nextInt(); String abilityString; switch (abilityPkmn) { case 1: abilityString = "Chlorophyll"; System.out.println("Boosts the Pokémon's Speed stat in harsh sunlight."); break; case 2: abilityString = "Guts"; System.out.println("Boosts the Pokemon's Attack stat when it has a status condition."); break; case 3: abilityString = "Cacophony"; System.out.println("The Pokemon is immune to sound-based moves."); break; case 4: abilityString = "Damp"; System.out.println("Prevents the use of Explosion and Self-Destruct."); break; case 5: abilityString = "Drought"; System.out.println("Activates harsh sunlight when the Pokémon enters battle."); break; default: abilityString = "Invalid ability"; break; } }public static void main(String[] args) { System.out.println("Enter Pokemon ability:"); Scanner scanner = new Scanner(System.in); String abilityPkmn = scanner.next(); //here no int because u are taking string String abilityString; //u do not need this anymore! switch (abilityPkmn) { case "Chlorophyll": System.out.println("Boosts the Pokémon's Speed stat in harsh sunlight."); break; case "Guts": System.out.println("Boosts the Pokemon's Attack stat when it has a status condition."); break; case "Cacophony": System.out.println("The Pokemon is immune to sound-based moves."); break; case "Damp": System.out.println("Prevents the use of Explosion and Self-Destruct."); break; case "Drought": System.out.println("Activates harsh sunlight when the Pokémon enters battle."); break; default: System.out.println("here comes any default text which is not included in all cases and there is no need for break;"); } }Me di cuenta de que no ha utilizado la declaración de impresión en el caso predeterminado. Utilice la instrucción println para obtener la salida en el dispositivo de salida estándar.
vote mi respuesta si esto ayuda.
Creo que necesitará identificar estas variables y luego escribir el código o la acción realizada en una nueva declaración.
Intente poner las declaraciones en una matriz y luego llámelas en un método.
Tal vez establezca una nueva clase llamada habilidades y cree algunos valores. Cuando los llama con un nuevo método, puede superclasificar esas habilidades.
IvonH en gitHub puedes ver lo que he hecho para un juego de ejército simple.