ここに新しいユーザーがいます。Javaでプライベートクラスを設定する
私は、オブジェクト指向プログラミングクラスのテキストベースのアドベンチャーゲームのための「フレームワーク」または草稿に取り組んできました。私は私のTAにそれを示し、彼はそれが良さそうだと言ったが、私は自分のプライベートクラスに運動を入れてみるべきである。なぜ私はこれを行う必要がありますか?そして私はそれをどのようにすべきか?
ご協力いただきありがとうございます。ここで
は私のメインクラスのための私のコードです:あなたはそれ自身のクラスで動きをつけたいと思う
public class Main {
public static void main(String args[]) {
// This is where we will build rooms
// Load inventory
ArrayList<String> inventory = new ArrayList<>();
// Start game
boolean playing = true;
while (playing) {
String input = Input.getInput();
// Movement commands
if (input.equals("north")) {
if (y > 0) {
y--;
Rooms.print(room, x, y);
} else {
System.out.println("You can't go that way.");
}
} else if (input.equals("south")) {
if (y < HEIGHT - 1) {
y++;
Rooms.print(room, x, y);
} else {
System.out.println("You can't go that way.");
}
} else if (input.equals("east")) {
if (x > 0) {
x--;
Rooms.print(room, x, y);
} else {
System.out.println("You can't go that way.");
}
} else if (input.equals("west")) {
if (x < WIDTH - 1) {
x++;
Rooms.print(room, x, y);
} else {
System.out.println("You can't go that way.");
}
}
// Look commands
else if (input.equals("look")) {
Rooms.print(room, x, y);
}
// Look at item commands
else if (input.equals("look thing")) {
//print item description
}
/* Take command
else if statement
Drop command
else if statement
*/
// Inventory commands
else if (input.equals("i") || input.equals("inv")
|| input.equals("inventory")) {
Inventory.print(inventory);
}
// Quit commands
else if (input.equals("quit")) {
System.out.println("Goodbye!");
playing = false;
// Catch-all for invalid input
} else {
System.out.println("Invalid input");
}
}
System.exit(0);
}
}
ようこそスタックオーバーフロー!あなたの最初の投稿でうまくいっています。私があなたに与えることができる1つのヒントは、あなたの 'System.out.println("あなたはそのように行くことはできません ");を関数にリファクタリングして呼び出すことです。この1行を数回繰り返すので、その文字列のすべての文字列を見つけるのではなく、1つの関数で簡単に後で変更することができます。 –
すべてのコマンドに対して 'input'で' switch'を使うことができます。それは少し明確です。 – bcsb1001
ロジックをすべてヤンクして別のクラスに入れる必要があります。その後、そのクラスのインスタンスをmainメソッドで作成し、プログラムを実行します。あなたのロジックとメインクラスは別々になります。私はあなたのTAが意味するものだと信じています。しかし、「プライベート」の部分についてはわからない。 – yogidilip