简体   繁体   English

从另一个都属于同一类的方法中调用一个方法

[英]Calling a method from another method in which both are in the same class

I am calling the method findRoom() which is in the class myClass from the main method: 我正在从main方法中调用类myClass的方法findRoom()

int room[]= {1,2,3,4,5,6,7,8,9,10};
String customer[] = {"","Shay","","Yan","Pan","","","Xiao","Ali",""};
    myClass m = new myClass();
    m.findRoom(customer, room);         

The class myClass is as follows: myClass如下:

class myClass {

int count = 0;

public void findRoom(String customerName[], int roomNo[]) {

    Scanner sc = new Scanner(System.in);
    System.out.println("Please enter Customer's Name");

    String name = sc.next();

    for (int i = 0; i < 10; i++) {

        if (customerName[i].equalsIgnoreCase(name)) {
            System.out.println(roomNo[i]);
            count++;
            break;
        } else {
            count++;

        }
    }
    myMethod(customerName, roomNo);
}

public void myMethod(String cusName[], int rooms[]) {
    myClass m = new myClass();
    if (m.count == 10) {
        System.out.println("Please check the name again");
        m.count = 0;
        m.findRoom(cusName, rooms);
    }
}
}

I want the program to prompt the user to enter the customer's name again, if the name entered by the user is not found in the array customer[] . 如果在数组customer[]找不到用户输入的名称,我希望程序提示用户再次输入客户的名称。 So I created the method myMethod() which will ask the user to re-enter customer's name. 因此,我创建了方法myMethod() ,该方法将要求用户重新输入客户的姓名。

The program runs fine if user enters a name that is already in the array, but doesn't work the other way around when user enters a name that is not found in the array. 如果用户输入数组中已有的名称,则程序运行良好,但是当用户输入数组中未找到的名称时,该程序将无法正常工作。 The method myMethod() is not being called. 未调用方法myMethod() What could be the possible reason for this? 这可能是什么原因? Is it a problem with the parameter passing? 参数传递有问题吗? Any help is appreciated. 任何帮助表示赞赏。 =) =)

Your mistake is that, when you go into myMethod , you create new myClass object and this object has count field, but value of this filed is zero, because this is new object. 您的错误是,当您进入myMethod ,创建了新的myClass对象,并且该对象具有count字段,但是该字段的值为零,因为这是新对象。 But all your work and changing count field going in another object that you create in main method: 但是,所有工作和更改count字段都将进入您在main方法中创建的另一个对象中:

myClass m = new myClass();
m.findRoom(customer, room); 

If you need so simple example, try to use static modifier on field count : 如果您需要如此简单的示例,请尝试对字段count使用static修饰符

static int count = 0;

Edit findRoom method: 编辑findRoom方法:

    myClass.count++;
    break;
} else {
    myClass.count++;

Edit myMethod method: 编辑myMethod方法:

if (myClass.count == 10) {
    System.out.println("Please check the name again");
    myClass.count = 0;
    m.findRoom(cusName, rooms);
}

First of all I suggest you to learn more about objects and classes. 首先,我建议您了解有关对象和类的更多信息。 In your code in method myMethod() first statement is for creating a new object of myClass . 在方法myMethod()代码中,第一条语句用于创建myClass的新对象。 When you create that it's like you are taking a fresh copy of your class's attributes. 当您创建它时,就好像您正在复制类的属性一样。 (If they are not static) So always it will give you variable count with the value you gave it, that is 0. (如果它们不是静态的),那么它将始终为您提供变量计数以及您赋予它的值,即0。
If your code has to remember the values given to class variables not depending on the objects you create you have to make them static. 如果您的代码必须记住赋予类变量的值而不取决于您创建的对象,则必须使它们成为静态的。 Then you can call those variables without creating objects. 然后,您可以在不创建对象的情况下调用这些变量。

myClass.count;

So what you have to do is just replace int count=0; 因此,您要做的就是替换int count=0; with static int count=0; static int count=0; and few more things to improve your coding. 还有几件事可以改善您的编码。

  • Start class names with capital letters (this is not a rule, but a good practice) 以大写字母开头的班级名称(这不是规则,但是一种很好的做法)
  • Learn more about difference between static and non-static methods/variables. 了解有关静态和非静态方法/变量之间差异的更多信息。
  • Normally class variables are used with private access modifier. 通常,类变量与private访问修饰符一起使用。 (for encapsulation) (用于封装)

Something like this should do the trick: 这样的事情应该可以解决问题:

import java.util.Scanner;
public class MyClass {


    static int[] room;
    static String[] customer;



    public static void main(String[] args) {

        room = new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        customer = new String[]{"", "Shay", "", "Yan", "Pan", "", "", "Xiao", "Ali", ""};

        MyClass mc = new MyClass();
        mc.findRoom(customer, room);

    }



    public void findRoom(String customerName[], int roomNo[]){

        Scanner sc = new Scanner(System.in);
        System.out.println("Please enter the Customer's name.");
        String name = sc.next();

        int count = 0;


        for(int i = 0; i < customerName.length; i++){
            if(customerName[i].equalsIgnoreCase(name)){

                System.out.println(name + " is in room number " + roomNo[i]);
                break;
            }
            else{
                count++;
            }
        }

        //##### RECURSION STARTS HERE #####
        if(count == customerName.length){

            count = 0;
            System.out.println("Name not found, please try again.\n");
            findRoom(customerName, roomNo);
        }

    }

}

Recursion (method calling on itself), saves you from having to create two methods, also using arrayName.length within the 'if statement' will avoid you having to hard code the condition if you decide to have a bigger set of arrays. 递归(方法本身调用)使您不必创建两个方法,并且在'if语句'中使用arrayName.length可以避免如果决定拥有更大的数组集时就不必对条件进行硬编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM