简体   繁体   English

为什么此代码两次调用方法?

[英]Why does this code call the methods twice?

I am writing a simple program. 我正在写一个简单的程序。 Seems easy enough. 似乎很容易。 The program is to ask the user what type of dwelling they live in, and how many hours they spend at home. 该程序将询问用户他们所住的房屋类型,以及他们在家里呆了多少小时。 Then we take the values the user entered and suggest a type of pet for them based on they're inputs. 然后,我们采用用户输入的值,并根据输入的值为他们建议一种宠物。

Now the question. 现在的问题。 Why is it that when I call the methods they are called twice. 为什么当我调用方法时,它们被调用了两次。 The first two methods are called twice and not the third. 前两个方法被调用两次,而不是第三个。 I suspect it is because the third method declared the a and b variable as the methods themselves but I cannot figure out why this is so, and not sure how to fix this bug. 我怀疑这是因为第三个方法将a和b变量声明为方法本身,但是我无法弄清楚为什么会这样,并且不确定如何解决此错误。

import javax.swing.JOptionPane;

public class PetAdvice {

public static int dwelling(){
    int a = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter 1 (Apartment), 2 (House), 3 (Dorm)","Dwelling", JOptionPane.QUESTION_MESSAGE));

    if(!(a == 1 || a == 2 || a == 3)){
        JOptionPane.showMessageDialog(null, "The value for dwelling must be 1 (Apartment), 2 (House), 3 (Dorm)","Dwelling type error", JOptionPane.ERROR_MESSAGE);
        System.exit(-1);
    }
    return a;
}

public static int hours(){
    int b = Integer.parseInt(JOptionPane.showInputDialog(null,"Enter the number of hours a week you are home.", JOptionPane.QUESTION_MESSAGE));

    if (b <= 0 || b >= 168){
        JOptionPane.showMessageDialog(null, "The number of hour per week you are home must be between 0  and 168 inclusivly.","Hours at home error.", JOptionPane.ERROR_MESSAGE);
        System.exit(-1);
    }
    return b;
}

public static void pet(int a, int b){
    a = dwelling();//Pretty sure these variables are declared wrong
    b = hours();
    if(a == 1 && b >= 10){
        JOptionPane.showMessageDialog(null, "You should get a cat!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
    }
    else
        if(a == 1 && b <= 10){
            JOptionPane.showMessageDialog(null, "You should get a Hamster!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
        }
        else 
            if(a == 2 && b > 18){
                JOptionPane.showMessageDialog(null, "You should get a Pot-bellied Pig!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
            }
            else
                if(a == 2 && b > 10 || b < 17){
                    JOptionPane.showMessageDialog(null, "You should get a dog!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
                }
                else
                    if(a == 3 && b >= 6){
                        JOptionPane.showMessageDialog(null, "You should get a fish!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
                    }
                    else
                        if(a == 3 && b < 6){
                            JOptionPane.showMessageDialog(null, "You should get an Ant farm!" ,"Reccomened Pet.", JOptionPane.INFORMATION_MESSAGE);
                        }


}


public static void main(String[] args) {
    dwelling();
    hours();
    //I don't know what parameters to call. Most likley due to the method variables. Can't figure out what to fix.
    pet(dwelling(), hours());
}

}

The methods are being called twice because your code calls them twice, once at the beginning of main, where you call them but then throw out the results returned: 方法被调用了两次,因为您的代码两次调用了它们,一次是在main的开头,您在其中调用了它们,但随后抛出了返回的结果:

    dwelling();
    hours();

And a 2nd time in the 3rd method's parameters: 第三遍方法的参数中第二遍:

    pet(dwelling(), hours());

Save the values returned from your method calls and pass them into the 3rd method. 保存从您的方法调用返回的值,并将它们传递给第三方法。 Don't recall them in the 3rd method parameter as you're doing. 请勿在执行操作时在第三方法参数中调用它们。 For example, change 例如改变

public static void main(String[] args) {
    dwelling();
    hours();
    pet(dwelling(), hours());
}

to

public static void main(String[] args) {
    int dwell = dwelling();
    int hrs = hours();
    pet(dwell, hrs);
}

And yeah, change 是的,改变

public static void pet(int a, int b){
   a = dwelling();//Pretty sure these variables are declared wrong
   b = hours();

to

public static void pet(int a, int b){
   // these guys below are completely unnecessary
   // a = dwelling();
   // b = hours();
pet(dwelling(), hours());

There is your problem. 有你的问题。 Try setting them to values first then sending those values to the pet() method. 尝试先将它们设置为值,然后将这些值发送到pet()方法。 Example: 例:

public static void main(String[] args) {
    ind dw = dwelling();
    int h = hours();
    //I don't know what parameters to call. Most likley due to the method variables. Can't figure out what to fix.
    pet(dw, h);
}

You yourself are calling the methods twice. 您自己调用了两次方法。 Every instance of dwelling() is a method call to that method. dwelling()每个实例都是对该方法的方法调用。 And finally, since you pass in the variables a, b into your pet(int a, int b) method, there is no reason to fetch them again by calling dwelling() and hours() 最后,由于您将变量a, b传递到pet(int a, int b)方法中,因此没有理由通过调用dwelling()hours()再次获取它们
To remove the redundant call in pet(a, b) : 要删除pet(a, b)中的冗余呼叫:

public static void pet(int a, int b){
    //remove these and just use the values passed in from main method
    //a = dwelling();
    //b = hours();
   ...
}

public static void main(String[] args) {
    pet(dwelling(), hours());
}

OR 要么

Keep the calls to dwelling() and hours() inside your pet() method, and change it to not need the two arguments (so you don't call them in you main method). 将对dwelling()hours()的调用保留在pet()方法中,并将其更改为不需要两个参数(因此,您不必在main方法中调用它们)。

public static void pet(){
    //call the methods here, instead of passing in their return values
    int a = dwelling();
    int b = hours();
    ...
}
public static void main(String[] args) {
    pet();
}

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

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