私はあなたが何百ものパワーアップを集めることができるこのゲームをプレイします、そしてそれらを覚えずに彼らが何をしているのかを知る唯一の方法はそれらを調べることです。そのため、パワーアップの名前を入力して効果を得ることができる「アシスタント」プログラムにしようとしています。
以下は、役に立たない部分を取り除いた完全なコードです。出力例を以下に示します。出力を見れば、問題はかなり明白なはずです。この問題を解決するためにどのような方法を使用できますか、または何が間違っていますか?
import java.util.ArrayList;
import java.util.Scanner;
public class ProgMain {
// // *** ------------*** and any variations indicate a change of importance or use
public static ArrayList<String> itemlist; // ArrayList of items to cycle through to see if item exists in database.
static {
itemlist = new ArrayList<String>();
itemlist.add("genericitem"); // testing item
itemlist.add("exit"); // allows for internal program exit
itemlist.add("debuglist"); // allows to print every item in "itemlist" for debug
// ***-------------------------------------------------***
itemlist.add(0, "thisitem hasaspace");
}
static Scanner console = new Scanner (System.in);
public static void main(String[] args) {
String item = " " // variable to store name of item
System.out.println("Enter an item name to check the database.");
System.out.println("Enter 'exit' to quit the program.");
System.out.println();
while(true) {
System.out.print("Item Name: ");
item = console.next().toLowerCase().replaceAll("\\s", " ");
/**
* ONLY FINDS FIRST WORD, even after removal of spaces.
*/
//DEBUG
System.out.print("\n DEBUG: " + item + "\n");
if (itemlist.contains(item)) { // cycle through database
displayItemProperties(item); // find item and print properties
System.out.println();
} else { // declares if the item isn't found in database
System.out.println("That item does not exist.");
System.out.println();
}
}
}
static void displayItemProperties(String item) {
/**
* Item names must be in LOWERCASE and have NO SPACES.
*/
// GENERIC IF STATEMENT
// Easy copy/paste to add new items
/*
if (item.equals("")) {
System.out.println("");
}
*/
// **----------------------------------**
if (item.equals("genericitem")) {
System.out.println("A generic item.");
}
if (item.equals("debuglist")) {
for (int i = 0; i < itemlist.size(); i++) {
System.out.println(itemlist.get(i));
}
}
if (item.equals("exit")) {
System.out.println("Application Terminating...");
System.exit(0);
}
// ***------------------------------------------------------***
if (item.equals("thisitem hasaspace")) {
System.out.println("If a name has a space, it wont show up correctly...");
}
// introduce new item declarations here
}
}
これは、スペースなしで入力されたものの出力例です。
Enter an item name to check the database.
Enter 'exit' to quit the program.
Item Name: genericitem
DEBUG: genericitem
A generic item.
Item Name: exit
DEBUG: exit
Application Terminating...
これは、スペース付きの名前が使用された場合に発生することです。
Enter an item name to check the database.
Enter 'exit' to quit the program.
Item Name: thisitem hasaspace
DEBUG: thisitem
That item does not exist.
Item Name:
DEBUG: hasaspace
That item does not exist.
Item Name: exit
DEBUG: exit
Application Terminating...