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

}

最佳答案

这些方法被调用两次,因为您的代码调用它们两次,一次在 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();

关于java - 为什么这段代码会调用这些方法两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28466113/

相关文章:

java - 在 Hibernate 中实现基于条件的搜索页面的优雅方式

java - JPA/Hibernate 映射 : “QuerySyntaxException: Player is not mapped…”

java - Hibernate 将默认日期时间插入非空列

java - 使 Java 编译器在类文件中包含符号常量字段引用 - 可能吗?

java - 当字符串大时,两个字符串的 XOR 不起作用

java - 在 Oracle 服务器上配置 IBMJCE 加密库

java - 语音识别API : How to get the voice features (MEL Coefficients)

java - 数据无法解析为类型

java - 超时后调用方法

Java JTree - 如何检查节点是否显示?