如何针对在ArrayList中随机生成的字符串元素验证用户输入?

时间:2017-07-11 15:54:35

标签: java validation arraylist random while-loop

我是java的新手,正在尝试构建我自己的基于文本的游戏,目前我只是尝试处理个别概念,然后希望能够在最终项目中将它们全部拉到一起,我的问题是目前遇到的似乎无法找到答案正是标题问题所说的:

如何针对随机生成的元素的String ArrayList验证用户输入?

为了澄清,我有一个工作程序,当运行时生成随机数量和随机类型的敌人,然后每次创建地下城对象时将它们填充到地牢中,然后向用户呈现允许的问题他们首先要挑选他们应该面对的敌人,这是我需要验证的地方,以便游戏继续进行,我试图使用while循环,如果条件返回false将跳过并继续if语句及其验证,我希望这是有道理的,我已经在下面发布了我的代码,如果我的代码有许多问题,无论是语法上还是结构上都有问题,如前所述,我仍然是非常新的java,而且如果我可以学习编码那么我可以担心应该有多么专业或适当的东西。

这是我的代码:

package com.davegreen;

import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class Main
{

    public static void main(String[] args)
    {
        // Declaring some variables and creating some objects of other classes that i need.

        Scanner scanner = new Scanner(System.in);
        Dungeon dungeon = new Dungeon();
        List<String> enemyType = dungeon.getEnemy();        // Gets all the enemy from my enemy ArrayList in the Dungeon class.
        int enemyNumberTotal = dungeon.getEnemy().size();   // Gets the amount of enemy in my enemy ArrayList.

        runGame();
        int randomAmountOfEnemy = generateRandomNumber(enemyNumberTotal);   // Generates a random amount of enemy into the dungeon from the total number in the ArrayList.

        System.out.println("\n The enemies in this dungeon are: \n");

        // Generates the random enemies that will populate the dungeon based on a method in my Dungeon class and then passes a random amount of said enemy each time also.

        List<String> randomlyPickedEnemy = dungeon.populateWithRandomEnemy(enemyType, randomAmountOfEnemy);

        System.out.println("\n\t * " + randomlyPickedEnemy);

        System.out.println("\n> Which enemy would you like to confront first?");
        String userChoice = scanner.nextLine();


        // I want to validate the user input from line 31 of the code to reflect that if the user has not selected an enemy that was in the randomly populated list
        // then we stay in the while loop and continue asking until of course the user types in a enemy that was in the randomly populated list, at which
        // point we would of course skip the while loop moving onto the if statements validation, it would be nice also to have ignore case somewhere in there.

        while(!userChoice.equals(randomlyPickedEnemy))
        {
            System.out.println("\n\t That enemy does not live in this dungeon!");
            System.out.println("\n> Which enemy would you like to confront first?");
            userChoice = scanner.nextLine();
        }

        // For the validation here i realise of course that just using the variable randomlyPickedEnemy in the sout would more than likely return ALL the enemy that
        // were randomly populated and not specifically the enemy that the user had chosen, so at this point i need a way to be able to access exactly what enemy it was
        // that the user had chosen to battle so that the sout statement makes sense but more importantly so i can then direct the code where it needs to go based on any given
        // particular enemy.

        if(userChoice.equals(randomlyPickedEnemy))
        {
            System.out.println("You have chose to battle " + randomlyPickedEnemy);
        }
    }

    public static void runGame()
    {
        System.out.println("##################################################");
        System.out.println("#####            DUNGEON CRAWLER             #####");
        System.out.println("##################################################");
    }

    // This is my method to generate a random amount of enemy for the total amount of enemy in my ArrayList.

    public static int generateRandomNumber(int howManyEnemies)
    {
        Random random = new Random();
        int rng = random.nextInt(howManyEnemies) + 1;
        System.out.println("\n There are " + rng + " enemies in this dungeon!");
        return rng;

    }
}

1 个答案:

答案 0 :(得分:0)

您实际上是在比较StringList(randomPickedEnemy)。 如果我理解正确,您想知道此列表是否包含用户的输入。 为此,您只需修改while声明:

while(!randomlyPickedEnemy.contains(userChoice)) {
    ...
}