【问题标题】:Trying to test input validity尝试测试输入有效性
【发布时间】:2018-01-19 03:14:37
【问题描述】:

我今天一直在做一个小项目,在 StackOverflow 和其他地方四处寻找答案后,我遇到了一个障碍。

我一直在尝试用 Java 创建一个选择你自己的冒险风格的游戏,我开始使用三个特征(姓名、种族、性别)创建一个角色。我计划使用这些来允许故事中的分支。

问题是我没有得到我想要的结果。目前,我已经让我的代码工作了,打印语句显示了玩家所做的选择。

//list out character description

    System.out.println("You are " + Name + ", a " + characterGender + " " + 
    characterRace + ".");

我让它正常工作,直到我尝试对给定的输入进行测试以根除无效条目。我尝试使用 do-while 循环和 try-catch 语句,但无论是否有有效输入,它都会向前运行到下一个选项。

我希望实现的总体思路是:

显示输入选项

检查输入以验证其是否有效

如果输入无效,请重新尝试检查其他输入

如果输入有效,则继续下一个问题

//create test to verify validity of race selection

    boolean raceBoolean;

    if (raceInt < 0 || raceInt > 4) {

        raceBoolean = true;

    try {

        do {

    //selection list that defines options

                if (raceInt == 1)
                    characterRace = "human";

                else if (raceInt == 2)
                    characterRace = "elf";

                else if (raceInt == 3)
                    characterRace = "orc";

                else if (raceInt == 4)
                    characterRace = "undead";

        }

        while (raceBoolean = false);

            System.out.println("Nice try. Enter a valid number for one of the four races!");

        }

            finally {

        }

老实说,我很茫然。我知道这很简单,但我正在努力自学。我感谢可能提供的任何帮助。其他主题并不是很有帮助,因为它们并没有提供太多背景来说明为什么会首先出现问题。

提前谢谢你。老实说,我正在努力学习如何正确地做到这一点。

完整代码如下:

import java.util.Scanner;

public class Adventure {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    //insert input tool
    Scanner input = new Scanner(System.in);

    //create inventory object for gold and items
    int gold = 10;

    //create health points object 

    //address player with greeting
    System.out.println("Welcome to your adventure!");

    //ask for character name, race, and gender

    System.out.println("What will your adventurer's name be?");

    String Name = input.nextLine();

    System.out.println("What will your adventurer's race be?");
    System.out.println("1. Human");
    System.out.println("2. Elf");
    System.out.println("3. Orc");
    System.out.println("4. Undead");

    int raceInt = input.nextInt();

    String characterRace = "";

    //create test to verify validity of race selection

    boolean raceBoolean;

    if (raceInt < 0 || raceInt > 4) {

        raceBoolean = true;

    try {

        do {

    //selection list that defines options

                if (raceInt == 1)
                    characterRace = "human";

                else if (raceInt == 2)
                    characterRace = "elf";

                else if (raceInt == 3)
                    characterRace = "orc";

                else if (raceInt == 4)
                    characterRace = "undead";

        }

        while (raceBoolean = false);

            System.out.println("Nice try. Enter a valid number for one of the four races!");

        }

            finally {

        }

    }

    //ask for character gender

    System.out.println("What will your adventurer's gender be?");
    System.out.println("1. Male");
    System.out.println("2. Female");

    int genderInt = input.nextInt();

    String characterGender = "";

    if (genderInt == 1)
        characterGender = "male";

    else if (genderInt == 2)
        characterGender = "female";

    //list out character description

    System.out.println("You are " + Name + ", a " + characterGender + " " + characterRace + ".");

    //introduce player to adventure with different introductions for different races

    System.out.print("You came to Glassolin with only ten gold pieces to your name. ");

    if (characterRace == "human")
        System.out.print("Glassolin is home to many humans; some from the far reaches of the realm. You feel right at home, more or less,"
                + " as long as you ignore the occasional odd look from the cityfolk staring at someone dressed in farmer's clothes.");
    else if (characterRace == "elf")
        System.out.print("While an elf isn't the most common sight around town, the locals seem like they don't care one way or another "
                + "about you wandering the streets. ");


}

}

【问题讨论】:

    标签: java loops try-catch do-while


    【解决方案1】:

    相比之下,您已经给出了while (raceBoolean=false)(赋值运算符)。其次是使用 == 运算符比较字符串。但是在java中字符串是使用.equals()方法比较的。我已经修改了你的代码。

    这是完整的工作代码

    import java.util.Scanner;
    
    public class Test {
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    
        //insert input tool
        Scanner input = new Scanner(System.in);
    
        //create inventory object for gold and items
        int gold = 10;
    
        //create health points object 
    
        //address player with greeting
        System.out.println("Welcome to your adventure!");
    
        //ask for character name, race, and gender
    
        System.out.println("What will your adventurer's name be?");
    
        String Name = input.nextLine();
    
    
    
        //create test to verify validity of race selection
    
        boolean raceBoolean=true;
        String characterRace = "";
    
    
    
        try {
    
            do {
    
        //selection list that defines options
                                     System.out.println("What will your adventurer's race be?");
                                    System.out.println("1. Human");
                                    System.out.println("2. Elf");
                                    System.out.println("3. Orc");
                                    System.out.println("4. Undead");
    
                                    int raceInt = input.nextInt();
                                    if (raceInt < 0 || raceInt > 4) {
                                    raceBoolean = false;
    
                                    }
                            else{
                                    raceBoolean = true;
                                }
    
                    if (raceInt == 1){
                        characterRace = "human";
    
                        }
    
                    else if (raceInt == 2)
                        characterRace = "elf";
    
                    else if (raceInt == 3)
                        characterRace = "orc";
    
                    else if (raceInt == 4)
                        characterRace = "undead";
                    System.out.println(raceInt);
    
            }
            while (raceBoolean==false);
            }
    
                finally {
    
            }
    
    
    
        //ask for character gender
        System.out.println("What will your adventurer's gender be?");
        System.out.println("1. Male");
        System.out.println("2. Female");
    
        int genderInt = input.nextInt();
    
        String characterGender = "";
    
        if (genderInt == 1)
            characterGender = "male";
    
        else if (genderInt == 2)
            characterGender = "female";
    
            System.out.println(characterRace);
        //list out character description
    
        System.out.println("You are " + Name + ", a " + characterGender + " " + characterRace + ".");
    
        //introduce player to adventure with different introductions for different races
    
        System.out.print("You came to Glassolin with only ten gold pieces to your name. ");
    
        if (characterRace.equals("human"))
            System.out.print("Glassolin is home to many humans; some from the far reaches of the realm. You feel right at home, more or less,"
                    + " as long as you ignore the occasional odd look from the cityfolk staring at someone dressed in farmer's clothes.");
        else if (characterRace.equals("elf"))
            System.out.print("While an elf isn't the most common sight around town, the locals seem like they don't care one way or another "
                    + "about you wandering the streets. ");
    
    
    }
    
    }
    

    【讨论】:

      【解决方案2】:

      开始于:

        System.out.println("4. Undead");
      

      此代码应继续要求提供有效答案,直到给出有效答案。

      //create test to verify validity of race selection  
      String characterRace = "";
      boolean raceBoolean = false;
      
      Do{
      int raceInt = input.nextInt();
      
          if (raceInt == 1){
              characterRace = "human";
              raceBoolean = true;
              }else{
                  if (raceInt == 2){
                  characterRace = "elf";
                  raceBoolean = true;
                  }else{
                      if (raceInt == 3){
                      characterRace = "orc";
                      raceBoolean = true;
                      }else{
                          if (raceInt == 4){
                          characterRace = "undead";
                          raceBoolean = true;
                          }else{
                              raceBoolean = false;
                              System.out.println("Nice try. Enter a valid number for one of the four races!");
                          }
                      }
                  }
              }
          }while(raceBoolean = false)
      

      它将输入放在 do while 语句中,这样它就可以反复请求不同的输入。此外,我将布尔值初始化为 false,就像故障保险一样。最后的 while 语句测试是否给出了有效的输入。

      使用嵌套的 if/else 语句完成测试。

      【讨论】:

        猜你喜欢
        • 2017-09-14
        • 2022-01-09
        • 1970-01-01
        • 2017-09-19
        • 2019-03-07
        • 1970-01-01
        • 2019-01-09
        • 2020-09-18
        • 2012-07-23
        相关资源
        最近更新 更多