Java 如何根据ArrayList中随机生成的字符串元素验证用户输入?

Java 如何根据ArrayList中随机生成的字符串元素验证用户输入?,java,validation,arraylist,random,while-loop,Java,Validation,Arraylist,Random,While Loop,我对java相当陌生,正在尝试构建我自己的基于文本的游戏,目前我只想研究单个概念,然后希望能够在最终项目中将其整合在一起,我目前遇到的问题似乎找不到答案,这正是标题问题所说的: 如何根据随机生成的元素的字符串数组列表验证用户输入 为了澄清,我有一个工作程序,当run生成随机数量和随机类型的敌人,然后在每次创建地下城对象时将其填充到地下城中,然后向用户提出一个问题,允许他们选择他们应该首先面对的敌人,这就是我需要验证的地方,这样游戏才能继续,我尝试使用while循环,如果条件返回false,它将跳

我对java相当陌生,正在尝试构建我自己的基于文本的游戏,目前我只想研究单个概念,然后希望能够在最终项目中将其整合在一起,我目前遇到的问题似乎找不到答案,这正是标题问题所说的:

如何根据随机生成的元素的字符串数组列表验证用户输入

为了澄清,我有一个工作程序,当run生成随机数量和随机类型的敌人,然后在每次创建地下城对象时将其填充到地下城中,然后向用户提出一个问题,允许他们选择他们应该首先面对的敌人,这就是我需要验证的地方,这样游戏才能继续,我尝试使用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;

    }
}
package com.davegreen;
导入java.util.List;
导入java.util.Random;
导入java.util.Scanner;
公共班机
{
公共静态void main(字符串[]args)
{
//声明一些变量并创建我需要的其他类的一些对象。
扫描仪=新的扫描仪(System.in);
地下城=新地下城();
List-enemyType=dungeon.get敌军();//从dungeon类中我的敌人阵列列表获取所有敌人。
int-enemyNumberTotal=dungeon.get敌军().size();//获取我的敌军阵列列表中的敌军数量。
runGame();
int randomAmountFenemy=generateRandomNumber(enemyNumberTotal);//从ArrayList中的总数生成随机数量的敌人进入地下城。
System.out.println(“\n此地牢中的敌人是:\n”);
//根据我的地下城类中的一个方法生成随机的敌人,然后每次随机传递一个数量的敌人。
列出RandomlyPickedEndemy=地牢。填充随机敌人(enemyType,RandomAmountEndemy);
System.out.println(“\n\t*”+randomlypeckedenemy);
System.out.println(“\n>您希望首先面对哪个敌人?”);
字符串userChoice=scanner.nextLine();
//我想验证来自代码第31行的用户输入,以反映如果用户没有选择随机填充列表中的敌人
//然后我们停留在while循环中,继续询问,直到用户输入随机填充列表中的敌人,在该列表中
//在这一点上,我们当然会跳过while循环,转到if语句验证,在其中的某个地方有ignorecase也是很好的。
而(!userChoice.equals(randomlyPickedEnemy))
{
System.out.println(“\n\t敌人不住在这个地牢里!”);
System.out.println(“\n>您希望首先面对哪个敌人?”);
userChoice=scanner.nextLine();
}
//对于这里的验证,我当然意识到,仅仅在sout中使用变量randomlyPickedEnemy很可能会返回所有的敌人
//是随机填充的,而不是用户选择的敌人,所以在这一点上,我需要一种方法来准确地访问它是什么敌人
//用户选择了战斗,这样sout语句才有意义,但更重要的是,这样我就可以根据任何给定的
//特定的敌人。
if(userChoice.equals(randomlyPickedEnemy))
{
System.out.println(“您选择了战斗”+随机选择);
}
}
公共静态void runGame()
{
3.系统。输出。系统。输出。印刷LN(系统。系统。系统。输出。系统。输出。印印印(“(“(“联合35)))系统。系统。系统。系统。系统。系统。系统。系统。系统。输出。系统。系统。输出。系统。输出。系统。印印民民民民民方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方####################;
System.out.println(“##############”;
3.系统。输出。系统。输出。印刷LN(系统。系统。系统。输出。系统。输出。印印印(“(“(“联合35)))系统。系统。系统。系统。系统。系统。系统。系统。系统。输出。系统。系统。输出。系统。输出。系统。印印民民民民民方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方方####################;
}
//这是我为ArrayList中的敌人总数生成随机数量敌人的方法。
公共静态int GeneratorDomainNumber(int HowmanyEnmies)
{
随机=新随机();
int rng=random.nextInt(多少敌人)+1;
System.out.println(“\n这个地牢里有“+rng+”个敌人!”);
返回rng;
}
}

您实际上是在将
字符串
列表
(随机选取)进行比较。 如果我理解正确,您想知道此列表是否包含用户的输入。 为此,您只需修改
while
语句:

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

非常感谢,当你知道怎么做很容易啊!,我仍然有一个问题,那就是找出用户选择了什么,然后相应地指导代码,但我现在要去尝试一下,我想我会处理不好的,谢谢你的帮助,救生员。