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

Java 为什么这段代码要两次调用这些方法?,java,Java,我正在写一个简单的程序。看起来很容易。该程序是询问用户他们住在什么类型的住宅中,以及他们在家里呆了多少小时。然后我们获取用户输入的值,并根据它们的输入为他们建议一种宠物类型 现在是问题。为什么当我调用这些方法时,它们会被调用两次。前两个方法调用两次,而不是第三个。我怀疑这是因为第三个方法将a和b变量声明为方法本身,但我不知道为什么会这样,也不知道如何修复这个错误 import javax.swing.JOptionPane; public class PetAdvice { public s

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

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

}
import javax.swing.JOptionPane;
公共类PetAdvice{
公共静态int住宅(){
int a=Integer.parseInt(JOptionPane.showInputDialog(null,“输入1(公寓)、2(房子)、3(宿舍)”、“住宅”,JOptionPane.QUESTION_消息));
如果(!(a==1 | | a==2 | | a==3)){
JOptionPane.showMessageDialog(null,“住宅的值必须是1(公寓)、2(房子)、3(宿舍)”,“住宅类型错误”,JOptionPane.error\u消息);
系统退出(-1);
}
返回a;
}
公共静态整时(){
int b=Integer.parseInt(JOptionPane.showInputDialog(null,“输入每周回家的小时数”,JOptionPane.QUESTION_MESSAGE));
如果(b=168){
JOptionPane.showMessageDialog(null,“每周在家的小时数必须介于0和168之间,包括0和168之间。”,“在家的小时数错误。”,JOptionPane.error_消息);
系统退出(-1);
}
返回b;
}
公共静态无效pet(内部a、内部b){
a=homing();//非常确定这些变量声明错误
b=小时();
如果(a==1&&b>=10){
showMessageDialog(null,“你应该得到一只猫!”,“重新饲养的宠物”,JOptionPane.INFORMATION\u MESSAGE);
}
其他的
如果(a==1&&b 18){
showMessageDialog(null,“你应该得到一只大腹便便的猪!”,“重新饲养的宠物”,JOptionPane.INFORMATION\u MESSAGE);
}
其他的
如果(a==2&&b>10 | | b<17){
showMessageDialog(null,“你应该养条狗!”,“重新饲养宠物”,JOptionPane.INFORMATION\u MESSAGE);
}
其他的
如果(a==3&&b>=6){
showMessageDialog(null,“你应该得到一条鱼!”,“重新饲养的宠物”,JOptionPane.INFORMATION\u MESSAGE);
}
其他的
如果(a==3&&b<6){
showMessageDialog(null,“你应该得到一个蚂蚁农场!”,“重新饲养的宠物”,JOptionPane.INFORMATION\u MESSAGE);
}
}
公共静态void main(字符串[]args){
居住();
小时();
//我不知道调用什么参数。最可能的原因是方法变量。无法确定要修复什么。
宠物(居住),小时数);
}
}

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

    dwelling();
    hours();
第三种方法参数中的第二次:

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

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

是的,改变

public static void pet(int a, int b){
   a = dwelling();//Pretty sure these variables are declared wrong
   b = 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);
}

您自己正在两次调用这些方法。
holding()
的每个实例都是对该方法的方法调用。最后,由于将变量
a,b
传递到
pet(inta,intb)
方法中,因此没有理由通过调用
holding()
hours()

要删除pet(a,b)中的冗余呼叫,请执行以下操作:


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


我试过了,结果还是一样。我认为这与pet方法有关。idk如何修复它。另外,更改这个:publicstaticvoidpet(inta,intb){a=homing();//非常确定这些变量声明错误b=hours();并删除这两行。A和B正在传入。它们不需要设置。我已经尝试过了,并得到了相同的结果。我认为这与pet方法有关。idk如何修复它。这也会产生相同的结果。看看pet方法。我认为其中有错误。我不知道会发生什么。
public static void pet(int a, int b){
   // these guys below are completely unnecessary
   // a = dwelling();
   // b = hours();
pet(dwelling(), hours());
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);
}
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());
}
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();
}