【问题标题】:Use user input for switch使用用户输入进行切换
【发布时间】:2021-09-30 02:06:55
【问题描述】:

对于我的 Java 类,我们必须检测 Pokemon 能力的用户输入,并使用 Switch 给出所述能力描述的输出。我有扫描仪来检测输入,但是当我输入除案例编号以外的任何内容时,它只会给我一个错误。我不知道如何让它检测到像“叶绿素”这样的能力,然后让它使用控制台的输出。

这是我的代码:

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;

    }
    
}

【问题讨论】:

  • 你输入的是数字还是字符串?
  • 最好在问题中添加错误信息。从您的问题来看,只要您不在之前或之后添加空格,从 1 到 5 的输入都可以正常工作。如果超过 5,那么您将不会收到错误,但您也不会看到任何结果。如果您输入除数字以外的任何内容,包括分数,您将得到异常。
  • int abilityPkmn = scanner.nextInt(); 为什么你认为它会接受除了int 之外的任何东西?
  • 你有没有尝试过?如果我理解正确,您的帖子似乎在说:“我编写了一个接受整数的程序。我不明白如何让它接受字符串。请为我编写代码。”对不起,但这不是它的工作原理。当您卡住时,我们会在这里回答问题,而不是为您编写您的代码。如果您尝试编写一个接受字符串的程序并遇到问题,那么该是发布到 Stack Overflow 的时候了。请参阅How to Ask

标签: java switch-statement


【解决方案1】:
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;");

    }
}

【讨论】:

    【解决方案2】:

    我注意到您在默认情况下没有使用 print 语句。使用 println 语句获取标准输出设备上的输出。

    如果有帮助,请投票给我的答案。

    【讨论】:

      【解决方案3】:

      我认为您需要识别这些变量,然后在新语句中写入代码或执行的操作。

      尝试将语句放入数组中,然后在方法中调用它们。

      也许设置一个名为能力的新类并创建一些值。当您以新方法调用它们时,您可以对这些能力进行超类化。

      IvonH 在 gitHub 你可以看到我为一个简单的军队游戏做了什么。

      【讨论】:

      • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-14
      相关资源
      最近更新 更多