为什么这段代码会调用两次方法?

时间:2015-02-11 22:52:10

标签: java

我正在写一个简单的程序。看起来很简单。该计划是询问用户他们住在哪种类型的住所,以及他们在家里花了多少小时。然后我们采用用户输入的值,并根据他们的输入为他们建议一种宠物。

现在的问题。为什么当我调用方法时,它们被调用两次。前两个方法被调用两次而不是第三个。我怀疑是因为第三种方法将a和b变量声明为方法本身,但我无法弄清楚为什么会这样,并且不确定如何修复这个bug。

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());
}

}

3 个答案:

答案 0 :(得分:1)

方法被调用两次,因为你的代码调用它们两次,一次在main的开头,你调用它们然后抛出返回的结果:

    dwelling();
    hours();

第3次方法的第2次参数:

    pet(dwelling(), hours());

保存方法调用返回的值,并将它们传递给第3个方法。当你正在做的时候,不要在第3个方法参数中回忆起它们。例如,更改

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

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

是的,改变

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

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

答案 1 :(得分:1)

pet(dwelling(), hours());

有你的问题。尝试先将它们设置为值,然后将这些值发送到pet()方法。 例如:

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);
}

答案 2 :(得分:0)

你自己两次调用这些方法。 dwelling()的每个实例都是对该方法的方法调用。最后,由于您将变量a, b传入pet(int a, int b)方法,因此无需再次通过调用dwelling()hours()来获取它们 要删除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

dwelling()方法中保持对hours()pet()的通话,并将其更改为不需要这两个参数(因此您不要在主要方法中调用它们) )。

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();
}