私はかなり新しいJavaに慣れています。自分のテキストベースのゲームを試しています。現時点では個々のコンセプトに取り組んで、最終的なプロジェクトですべてを一緒に取り込めるようにしたいと思っています。現在のところ、答えを見つけることができないようです。タイトルの質問には、正確に何が書かれていますか?ArrayListでランダムに生成された文字列要素に対するユーザー入力を検証する方法は?
ランダムに生成された要素のString ArrayListに対するユーザー入力を検証する方法はありますか?
明確にするために、実行時にランダムな量のランダムな種類の敵が生成され、次にダンジョンオブジェクトが作成されるたびにそれらをダンジョンに投入する作業プログラムがあります。彼らは最初に対決する必要がある敵を選ぶことができます。これは、ゲームが続行されるように検証が必要なところです。whileループを使用しようとすると、条件がfalseを返すとif文と妥当性確認、私はそれが理にかなっていることを願っています。もし私がコードを学ぶことができればそれで私は専門家や適切なものがどうあるべきか心配することができます。
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;
}
}
おかげで多く、その簡単なのあなたはえっ!、IVEはまだユーザーが選択した正確に何を見つけ出すと、コードを向けるの問題を得た方法を知っているとき:あなたは自分の
while
ステートメントを変更する必要があり、これを行うには それにもかかわらず、私は行こうとしている今、それを刺すと病気の管理、あなたの助けに感謝、救命救助者だと思う。 –