Home > Software engineering >  Use user input for switch
Use user input for switch

Time:09-30

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;

    }
    
}

CodePudding user response:

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

    }
}

CodePudding user response:

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.

  • Related