Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

228
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda