有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么这段代码要两次调用这些方法?

我正在写一个简单的程序。看起来很容易。该程序是询问用户他们住在什么类型的住宅中,以及他们在家里呆了多少小时。然后我们获取用户输入的值,并根据它们的输入为他们建议一种宠物类型

现在是问题。为什么当我调用这些方法时,它们会被调用两次。前两个方法调用两次,而不是第三个。我怀疑这是因为第三个方法将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());
}

}

共 (3) 个答案

  1. # 1 楼答案

    你自己正在两次调用这些方法。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());
    }
    

    或者

    将对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();
    }
    
  2. # 2 楼答案

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

        dwelling();
        hours();
    

    第三种方法的参数中有第二次:

        pet(dwelling(), hours());
    

    保存方法调用返回的值,并将其传递到第三个方法。不要在第三个方法参数中回忆它们。例如,改变

    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();
    
  3. # 3 楼答案

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