Java 当用户输入不正确的值时,如何循环案例结构

Java 当用户输入不正确的值时,如何循环案例结构,java,Java,请注意,这只是代码的一部分。我需要做的是在用户输入无效输入时循环整个案例部分。最好的方法是什么,while条件还是do-while。此外,我想知道我是否可以缩短这个案例,就像使用任何其他方法而不是使用案例一样。(如使用for循环) 要允许用户犯错误并再次询问他,您可以使用do while循环,它将首先询问用户,如果条件仍然匹配(输入不正常),您可以再次询问 您最好使用列表来存储名称,因为它允许您查找元素并直接获取数组的索引(如果添加更多元素,则不必向开关添加多个案例) 不要将\t与名称一起存储,

请注意,这只是代码的一部分。我需要做的是在用户输入无效输入时循环整个案例部分。最好的方法是什么,while条件还是do-while。此外,我想知道我是否可以缩短这个案例,就像使用任何其他方法而不是使用案例一样。(如使用for循环)


  • 要允许用户犯错误并再次询问他,您可以使用
    do while
    循环,它将首先询问用户,如果条件仍然匹配(输入不正常),您可以再次询问
  • 您最好使用
    列表
    来存储名称,因为它允许您查找元素并直接获取数组的索引(如果添加更多元素,则不必向开关添加多个
    案例
  • 不要将
    \t
    与名称一起存储,因为它不是名称的一部分,而是
    打印的一部分,所以在打印时使用它
  • 请命名您的
    方法
    以下约定:lowerCamelCase+有效名称

下面是如何实现它的,while
中的条件((index=chaName.indexOf(characterClass))==-1)
表示如果输入的
索引是
-1
,则表示找不到,因此无效,因此您将再次询问

public static int Class() {

     System.out.println("Character Types");

     String []chaName = new String[]{"\tWizard", "\tSorcerer", "\tMonk", "\tCleric", "\tWarlock", 
               "\tDruid", "\tRouge", "\tBard", "\tFighter", "\tRanger", "\tPaladin", "\tBarbarian"}; 
     int []characterDiceValue = new int[]{6, 6, 8, 8, 8, 8, 8, 8, 10, 10, 10, 12};

     Arrays.stream(chaName).forEach(System.out::println);

     System.out.println("Select a Character name from the given list above");
     characterClass = sc.next();

     switch(characterClass) {
     case "Wizard" :
     case "wizard" : hitDice = characterDiceValue[0];
     break;
     case "Sorcerer" :
     case "sorcerer" : hitDice = characterDiceValue[1];
     break;
     case "Monk" :
     case "monk" : hitDice = characterDiceValue[2];
     break;
     case "Cleric" :
     case "cleric" : hitDice = characterDiceValue[3];
     break;
     case "Warlock" :
     case "warlock" : hitDice = characterDiceValue[4];
     break;
     case "Druid" :
     case "druid" : hitDice = characterDiceValue[5];
     break;
     case "Rouge" :
     case "rouge" : hitDice = characterDiceValue[6];
     break;
     case "Bard" :
     case "bard" : hitDice = characterDiceValue[7];
     break;
     case "Fighter" :
     case "fighter" : hitDice = characterDiceValue[8];
     break;
     case "Ranger" :
     case "ranger" : hitDice = characterDiceValue[9];
     break;
     case "Paladin" :
     case "paladin" : hitDice = characterDiceValue[10];
     break;
     case "Barbarian" :
     case "barbarian" : hitDice = characterDiceValue[11];
     break;
     default : System.out.println("Invalid input. Enter again");
     characterClass = sc.next();
     break;
     }   
     return hitDice;

 }

您不会对每个“开关测试”使用案例“testval”:两次,

使用
do while
loop我建议使用
Map
而不是
switch
,并使用
String.toLowerCase
来消除噪音。然后只需在
while中包装整个批次(hitDice<0)
循环。为什么要使用并行集合而不是
映射
。您已经将高效的OPs代码变成了极为低效的代码。对于
映射
,这是微不足道的-使用其中一个,或者坚持使用
开关
@Boristeider是的,可能有更好的方法。但我仍然是编程初学者撞击,仍在学习java。正如我所说,这只是我正在编写的代码的一部分,需要做很多改进。地图对我来说是一个新概念,所以我将学习它并使用它来改进代码。感谢您的反馈。我很感激。这并不能回答问题。
public static int Class() {
    System.out.println("Character Types");

    List<String> chaName = Arrays.asList("Wizard", "Sorcerer", "Monk", "Cleric", "Warlock",
            "Druid", "Rouge", "Bard", "Fighter", "Ranger", "Paladin", "Barbarian");
    int[] characterDiceValue = new int[]{6, 6, 8, 8, 8, 8, 8, 8, 10, 10, 10, 12};

    chaName.forEach(s -> System.out.println("\t" + s));
    int index = -1;
    String characterClass;

    do {
        System.out.println("Select a Character name from the given list above");
        characterClass = sc.nextLine();
    } while ((index = chaName.indexOf(characterClass)) == -1);

    return characterDiceValue[index];

}
public static int Class() {
    System.out.println("Character Types");

    Map<String, Integer> data = new HashMap<>();
    data.put("Wizard", 6);   data.put("Sorcerer", 6);   data.put("Monk", 8);
    data.put("Cleric", 8);   data.put("Warlock", 8);    data.put("Druid", 8);
    data.put("Rouge", 8);    data.put("Bard", 8);       data.put("Fighter", 10);
    data.put("Ranger", 10);  data.put("Paladin", 10);   data.put("Barbarian", 12);

    data.keySet().forEach(s -> System.out.println("\t" + s));
    int value = -1;
    String choice;

    do {
        System.out.println("Select a Character name from the given list above");
        choice = sc.nextLine();
    } while ((value = data.getOrDefault(choice, -1)) == -1);

    return value;
}