2

だから私はTUIをやっていて、これが私の最初の繰り返しでした。

package bulb.classes;

import java.util.Scanner;
import java.util.ArrayList;

public class RoomTUI {

private ArrayList<Room> rooms;
Scanner scan = new Scanner (System.in);
private int userNumber;
private String userAnswer;

public void run() {
    rooms = new ArrayList<Room>();
    introduction();
    userNumber = 0;
    options();
    while(userNumber < 5) {
        if(userNumber == 1) {
            newRoom();
        }
        if(userNumber == 2) {
            addBulbToRoom();
        }
        if(userNumber == 3) {
            clickAllBulbsInRoom();
        }
        if(userNumber == 4) {
            printDescriptionOfBulbs();
        }
    }
    System.out.println("Goodbye");
}

public int getUserInt(String aString) {
    System.out.println(aString);
    userAnswer = scan.nextLine();
    userNumber = Integer.parseInt(userAnswer);
    return userNumber;
}

public void displayRooms() {
    System.out.println("Possible rooms to choose from.");
    String tempString = "";
    int roomIndex = 0;
    for (int i = 0; i < rooms.size(); i++) {
        tempString = tempString + "Room " + roomIndex++ + ": " + rooms.get(i).getDescription() + "\n";
    }
    System.out.println(tempString);
}

public void introduction() {
    System.out.println("Welcome! With this program you can make rooms and design and place the light bulbs for each room you create.");
}

public void options() {
    System.out.println("1 : Create a new Room");
    System.out.println("2 : Add a bulb to an existing room");
    System.out.println("3 : Click all of the bulbs in a particular room");
    System.out.println("4 : Display a description of all bulbs in a particular room");
    System.out.println("5 : Quit");
    getUserInt("What would you like to do?");
}

public void newRoom() {
    System.out.println("Please enter a name for your room");
    String name = scan.nextLine();
    Room aRoom = new Room(name);
    rooms.add(aRoom);
    System.out.println("You have added the " + name + ".");
    options();
}

public void addBulbToRoom() {
    displayRooms();
    System.out.println("Which room do you want the bulb in?");
    String choice = scan.nextLine();
    int choiceNumber = Integer.parseInt(choice);
    System.out.println("Please enter the blub's color.");
    String color = scan.nextLine();
    System.out.println("Please enter the blub's increment amount.");
    String incrementS = scan.nextLine();
    int incrementI = Integer.parseInt(incrementS);
    ThreeWayBulb aBulb = new ThreeWayBulb(color, incrementI);
    rooms.get(choiceNumber).addBulb(aBulb);
    System.out.println("A " + color + " bulb with and increment of " + incrementI + " was added.");
    options();
}

public void clickAllBulbsInRoom() {
    displayRooms();
    System.out.println("Which room do you want the bulbs clicked?");
    String choice = scan.nextLine();
    int choiceNumber = Integer.parseInt(choice);
    rooms.get(choiceNumber).clickAllBulbs();
    System.out.println("The bulbs in " + rooms.get(choiceNumber).getDescription() + " have been clicked.");
    options();
}

public void printDescriptionOfBulbs() {
    displayRooms();
    System.out.println("Please enter a room number.");
    String choice = scan.nextLine();
    int choiceNumber = Integer.parseInt(choice);
    System.out.println(rooms.get(choiceNumber).getDescription() + " with " + rooms.get(choiceNumber).returnSize() + " bulbs: " + "\n" + rooms.get(choiceNumber).toString());
    options();
 }
}

私のインストラクターは、インスタンス変数なしでこれを行うことを望んでいます。彼は、メソッドに ArrayList が必要な場合は、それをパラメーターにして、TUI にインスタンス変数を持たないようにする必要があると言いました。私は一生、それを行う方法を理解できません。また、スタティックワークフライにすることも。ご協力いただきありがとうございます。

4

2 に答える 2

3

彼は、中央の場所 (メイン スレッドなど) から ArrayList を宣言し、それを使用する関数に引数として渡すことを望んでいます。このように、メソッドを取得して別のクラスに配置した場合、メソッドはこのクラスに依存していないため、壊れることはありません。

たとえば、あなたのnewRoomクラスを取る場合:

public void newRoom(List<Room> roomList) {
    System.out.println("Please enter a name for your room");
    String name = scan.nextLine();
    Room aRoom = new Room(name);
    roomList.add(aRoom);
    System.out.println("You have added the " + name + ".");
    options();
}

編集:これを達成する最も簡単な方法は、おそらく to の宣言をメソッドrooms内に移動するrunことです。「unknown variable rooms」を報告するコード内の各場所に対して、ArrayList をパラメーターとして受け取るように関数を変更できます。

于 2011-10-31T22:25:53.207 に答える
1

まあ、メンバーとして userNumber と userAnswer を削除するのは簡単です。それらの使用法は非常にローカライズされています。

リストについては、メイン ループで作成した後に渡すだけです。

スキャナーは複数の場所で使用されます。渡すこともできると思います。

于 2011-10-31T22:30:11.707 に答える