Java 我需要在不使用实例变量的情况下重新构造这个类

Java 我需要在不使用实例变量的情况下重新构造这个类,java,parameters,instance-variables,tui,Java,Parameters,Instance Variables,Tui,所以我在做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;

所以我在做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();
 }
}
package.class;
导入java.util.Scanner;
导入java.util.ArrayList;
公共教室{
私人阵列式房间;
扫描仪扫描=新扫描仪(System.in);
私有int用户号;
私有字符串userAnswer;
公开募捐{
房间=新的阵列列表();
导言();
userNumber=0;
选项();
while(用户号<5){
if(userNumber==1){
纽瓦姆();
}
if(userNumber==2){
addBulbToRoom();
}
if(userNumber==3){
单击所有bulbsinroom();
}
if(userNumber==4){
printDescriptionOfBulbs();
}
}
System.out.println(“再见”);
}
public int getUserInt(字符串aString){
系统输出打印项次(aString);
userAnswer=scan.nextLine();
userNumber=Integer.parseInt(userAnswer);
返回用户号;
}
公共休息室(){
System.out.println(“可供选择的房间”);
字符串tempString=“”;
int-roomIndex=0;
对于(int i=0;i

我的指导老师希望我在没有实例变量的情况下这样做,他说,如果一个方法需要ArrayList,我应该将它作为一个参数,并且在我的TUI中没有实例变量。我一辈子都不知道该怎么做。此外,使其静态工作飞行也。感谢您提供的帮助。

他希望您从中心位置(如主线程)声明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();
}
public void newRoom(列表roomList){
System.out.println(“请输入您房间的名称”);
字符串名称=scan.nextLine();
Room aRoom=新房间(名称);
房间列表。添加(aRoom);
System.out.println(“您添加了“+name+”);
选项();
}

编辑:实现这一点的最简单方法可能是将
rooms
的声明移动到
run
方法中。现在,对于代码中报告“未知变量室”的每个位置,您可以修改函数,将ArrayList作为一个参数。

好吧,删除userNumber和userAnswer作为成员并不重要;它们的用法非常本地化

对于列表,只需在主循环中创建后传递它


扫描仪用于多个位置;我想它也可以传给别人。

我觉得是这样的。那么我必须让run方法返回ArrayList吗?这是我唯一能想到的传递它的方法。是的,你需要让函数返回List类。我想是这样的。那么我必须让run方法返回ArrayList吗?这是我唯一能想到的让它传开的方法。@LordCanti不,这是你的“事件循环”,因为没有更好的词了。它会将它传递给所有需要访问它的方法。非常感谢。“我明白我现在该怎么做了。”洛德坎蒂:没问题。记住投票并接受答案,这样将来有类似问题的访问者知道解决方案是什么!国际海事组织(IMO)应该接受该委员会的回答,因为它提供了更多细节(但请随意投票支持我的答案),但至少接受另一个答案。我会投票支持,但它说我需要15岁的声誉才能做到这一点。