Methods system.out.print是否在主方法之外?

Methods system.out.print是否在主方法之外?,methods,output,static-methods,Methods,Output,Static Methods,我无法让“system.out.print”在DisplayNumberPlus 10、DisplayNumberPlus 100、DisplayNumberPlus 1000方法中工作。我做错什么了吗 public class ArithmeticMethods { public static void main(String[] args){ int cats = 46; int dogs = 25; } public s

我无法让“system.out.print”在DisplayNumberPlus 10、DisplayNumberPlus 100、DisplayNumberPlus 1000方法中工作。我做错什么了吗

public class ArithmeticMethods {

    public static void main(String[] args){ 
        int cats = 46;
        int dogs = 25;

    }
        public static void displayNumberPlus10(){ 
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 10 more."
                + (cats + 10) + ".");
        System.out.print("There were" + dogs + "now we will add 10 more."
                + (dogs + 10) + ".");
        }
    public static void displayNumberPlus100(){
        int cats = 46;
        int dogs = 25;
        System.out.print("There were" + cats + "now we will add 100 more "
                + (cats + 100) + ".");
        System.out.print("There were" + dogs + "now we will add 100 more "
                + (dogs + 100) + ".");
    }
    public static void displayNumberPlus1000(){
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 1000 more "
                + (cats + 1000) + ".");
        System.out.print("There were" + dogs + "now we will add 1000 more."
                + (dogs + 1000) + ".");
}
}

您没有调用任何方法。您需要在main方法中调用方法来执行它们,然后它将打印出来

public class ArithmeticMethods {

    public static void main(String[] args){ 
        int cats = 46;
        int dogs = 25;
        //Call your methods as given below
        displayNumberPlus10();
        displayNumberPlus100();
        displayNumberPlus1000();    
    }

    public static void displayNumberPlus10(){ 
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 10 more."
            + (cats + 10) + ".");
        System.out.print("There were" + dogs + "now we will add 10 more."
            + (dogs + 10) + ".");
    }

    public static void displayNumberPlus100(){
        int cats = 46;
        int dogs = 25;
        System.out.print("There were" + cats + "now we will add 100 more "
            + (cats + 100) + ".");
        System.out.print("There were" + dogs + "now we will add 100 more "
            + (dogs + 100) + ".");
    }

    public static void displayNumberPlus1000(){
        int cats = 46;
        int dogs = 25;

        System.out.print("There were" + cats + "now we will add 1000 more "
            + (cats + 1000) + ".");
        System.out.print("There were" + dogs + "now we will add 1000 more."
            + (dogs + 1000) + ".");
    } 
}
你可以试试这个

class ArithmeticMethods {

    public static void main(String[] args){ 
        int cats = 46;
        int dogs = 25;

    displayNumberPlus10(cats,dogs);
    displayNumberPlus100(cats,dogs);
    displayNumberPlus1000(cats,dogs);
    }
        public static void displayNumberPlus10(int c,int d){ 
        int cats = c;
        int dogs = d;

        System.out.print("There were" + cats + "now we will add 10 more."
                + (cats + 10) + ".");
        System.out.print("There were" + dogs + "now we will add 10 more."
                + (dogs + 10) + ".");
        }
    public static void displayNumberPlus100(int c,int d){
        int cats = c;
        int dogs = d;
        System.out.print("There were" + cats + "now we will add 100 more "
                + (cats + 100) + ".");
        System.out.print("There were" + dogs + "now we will add 100 more "
                + (dogs + 100) + ".");
    }
    public static void displayNumberPlus1000(int c,int d){
        int cats = c;
        int dogs = d;

        System.out.print("There were" + cats + "now we will add 1000 more "
                + (cats + 1000) + ".");
        System.out.print("There were" + dogs + "now we will add 1000 more."
                + (dogs + 1000) + ".");
}
}

您必须在main方法中调用这些方法。在main方法中添加以下代码

displayNumberPlus10();
displayNumberPlus100();
displayNumberPlus1000();
另外,为了更好地增强代码,不要在每个方法中都编写此代码:

int cats = 46;
int dogs = 25;
你可以在所有方法中传入猫和狗,也可以在主方法中调用猫和狗时传入猫和狗。所以它看起来像这样:

public class ArithmeticMethods {

public static void main(String[] args){ 
    int cats = 46;
    int dogs = 25;
    displayNumberPlus10(cats, dogs);
    displayNumberPlus100(cats, dogs);
    displayNumberPlus1000(cats, dogs);

}
    public static void displayNumberPlus10(int cats, int dogs){ 


    System.out.println("There were" + cats + "now we will add 10 more."
            + (cats + 10) + ".");
    System.out.println("There were" + dogs + "now we will add 10 more."
            + (dogs + 10) + ".");
    }
public static void displayNumberPlus100(int cats, int dogs){

    System.out.println("There were" + cats + "now we will add 100 more "
            + (cats + 100) + ".");
    System.out.println("There were" + dogs + "now we will add 100 more "
            + (dogs + 100) + ".");
}
public static void displayNumberPlus1000(int cats, int dogs){


    System.out.println("There were" + cats + "now we will add 1000 more "
            + (cats + 1000) + ".");
    System.out.println("There were" + dogs + "now we will add 1000 more."
            + (dogs + 1000) + ".");
} }


希望对您有所帮助。

您是否调用过
displayNumberPlus10()
或其他两种方法?你的主要方法没有。如果你不打电话给他们,那将是一个看不到指纹“起作用”的理由。