Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

227
Vistas
Use user input for switch

For my Java class, we have to detect user input of a Pokemon ability and give an output of the description of said ability using a Switch. I have the scanner to detect an input, but when I type anything other than the case numbers then it just gives me an error. I don't know how to make it detect an ability like 'Chlorophyll,' and then have it use the output to the console.

Here is my code:

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;

    }
    
}
over 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

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;");

    }
}
over 4 years ago · Santiago Trujillo Denunciar

0

I have noticed that you haven't used the print statement in the default case. Use the println statement to get the output on the standard output device.

vote my answer if this helps.

over 4 years ago · Santiago Trujillo Denunciar

0

I think you will need to identify these variables and then write in the code or action performed in a new statement.

Try putting the statements into an array then call them in a method.

Perhaps set up a new class called abilities and create some values. When you call them in a new method you can super class those abilities.

IvonH on gitHub you can see what I have done for a simple army game.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda