在方法中使用对象

时间:2014-03-19 14:06:31

标签: java object

我正在尝试使用一个可以从我的数组myHotel []删除客户的菜单系统,这是从一个对象构建的。

if(menu.charAt(0) == 'D')deleteCustomer(myHotel[]);

...

public void deleteCustomer(String myHotel[]){
         Scanner input = new Scanner(System.in);
         System.out.println("Please Enter Room Number to Delete Customer");
         roomNum=input.nextInt();
         myHotel[roomNum].setName("e");
    }

我收到错误,找不到符号?

这是完整代码

import java.util.*;
public class Main {

     public static void main(String[] args) {

        Scanner input = new Scanner(System.in);
        int roomNum=0;
        Room[] myHotel = new Room[10];
        for (int x =0; x<10; x++){
           myHotel[x] = new Room(); 
        }

        String roomName;

        String menu;
        do { 

        System.out.println("Please Select an Option from the Menu:");
        System.out.println("Enter V to View all Rooms");
        System.out.println("Enter A to Add Customer to Room");
        System.out.println("Enter D to Delete Customer from Room");
        System.out.println("Enter Q to Quit");
        menu=input.next();
        //if(menu.charAt(0) == 'V')viewAllRooms(); 
        //if(menu.charAt(0) == 'A')addCustomer(); 
        if(menu.charAt(0) == 'D')deleteCustomer(myHotel[]);
        } while (menu.charAt(0) != 'Q');

            while (roomNum < 10) {
            for (int x = 0; x < 10; x++ )
             if (myHotel[x].getName().equals("e"))System.out.println("room " + x + " is empty");

            System.out.println("Enter room number (0-9) or 10 to stop:");
            roomNum = input.nextInt();
            System.out.println("Enter name for room " + roomNum + " :");
            roomName = input.next();
            myHotel[roomNum].setName(roomName);

             for (int x = 0; x < 10; x++) {
                //System.out.println("room " + x + " occupied by " + myHotel[x].mainName);
                System.out.println("room " + x + " occupied by " + myHotel[x].getName());
            }
        }
    }
     public void deleteCustomer(String myHotelRef){
         Scanner input = new Scanner(System.in);
         System.out.println("Please Enter Room Number to Delete Customer");
         int deleteRoom=input.nextInt();
         myHotelRef[deleteRoom].setName("e");
    }

     }

3 个答案:

答案 0 :(得分:5)

您收到多个错误。什么是myHotel[]roomNum未定义,等等。

请使用您的编译器。

另外:请阅读https://stackoverflow.com/help/how-to-ask: - )

答案 1 :(得分:3)

首先,您需要声明myHotel数组并将其传递出[]

deleteCustomer(myHotel);

其次,String类

中没有这样的方法setName(String name)
myHotel[roomNum].setName("e");// no such a method

第三,您需要声明roomNum变量,如:

int roomNum = input.nextInt();

答案 2 :(得分:0)

您的主要问题是,您[]的来电中包含deleteCustomer应该

if (menu.charAt(0) == 'D') {
    deleteCustomer(myHotel);
}

当您引用整个数组对象时,不要包含方括号。方括号用于声明,初始化和访问数组中的各个元素。

我还建议您养成使用ifforwhile结构的花括号的习惯,因为不包括它们通常是原因虫子它还使您在回到它时更容易阅读,并且您清楚地向其他人说明应该是循环的一部分以及不应该做什么。