このプロジェクトの目的は、ユーザー入力によって渡されたすべてのポケモンを追加して保持するpokedexを作成することです。ユーザがポケモンに既に格納されているポケモンを入力すると、「複製」という単語がコンソールに印刷されるはずである。オブジェクト配列内に実際の複製が存在しない場合でも、duplicateという単語が出力されます。コンソールからの出力は次のとおりです。オブジェクトの配列で重複を見つける
新しいPokeDexへようこそ! あなたの地域にはポケモンの数はいくつですか?:3
あなたの新しいポケモンは3ポケモンを保持できます。それを使い始めましょう!
- 一覧ポケモン
- ポケモン
- チェックを追加ポケモンの統計
- ソートポケモン
- 出口
何をしますか? 2
ポケモンの種を入力してください:赤 重複
今ここに、おそらく次のクラスでは、このエラー
import java.util.Scanner;
public class Project4 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to your new PokeDex!");
System.out.print("How many Pokemon are in your region?: ");
int size = input.nextInt();
Pokedex pokedex = new Pokedex(size);
System.out.println("\nYour new Pokedex can hold " + size + " Pokemon. Let's start using it!");
int choice = 0;
boolean done = false;
while (!done) {
System.out.println("\n1. List Pokemon\n2. Add Pokemon\n3. Check a Pokemon's Stats" + "\n4. Sort Pokemon\n5. Exit");
System.out.print("\nWhat would you like to do? ");
choice = input.nextInt();
switch (choice) {
case 1:
String[] pokemonList = pokedex.listPokemon();
if (pokemonList == null)
System.out.println("Empty");
else
for (int i = 0; i < pokemonList.length; i++) {
System.out.println((i + 1) + ". " + pokemonList[i]);
}
break;
case 2:
System.out.print("\nPlease enter the Pokemon's Species: ");
String species = input.next();
pokedex.addPokemon(species);
break;
}
}
}
}
を作り、私が追加されます実際のメソッドを持ってすることができ使用されているすべてのコードがありますポケモンとPokedexのコンストラクタ
public class Pokedex {
Pokemon[] pokedex;
String pokeArray[];
public Pokedex(int size) {
pokedex = new Pokemon[size];
pokeArray = new String[size];
}
public boolean addPokemon(String species) {
Pokemon stuff = new Pokemon(species);
for (int i = 0; i < pokedex.length; i++) {
if (pokedex[i] == null) {
pokedex[i] = stuff;
}
else if (i < pokedex.length && pokedex[i] != null) {
System.out.println("Max");
}
if (pokedex[i].getSpecies().equalsIgnoreCase(species)) {
System.out.print("Duplicate");
break;
}
}
return false;
}
}
大量のコードについて申し訳ありませんが、予想される結果が出ている。
[宿題に関する質問と回答はどうすればいいですか?](http://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions)を参照してください。 – lexicore