Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从main方法调用具有可变参数的方法?_Java_Variables_Methods_Parameters_Call - Fatal编程技术网

Java 如何从main方法调用具有可变参数的方法?

Java 如何从main方法调用具有可变参数的方法?,java,variables,methods,parameters,call,Java,Variables,Methods,Parameters,Call,我想从我的主方法调用isATens方法,但我只能在isATens没有参数的情况下这样做。我试着在调用者中输入相同的参数,但这似乎也无法识别 public class P1L4 { public static void main(String[] args) { P1L4 main = new P1L4(); main.run(); isATens(userInput); //<--- this is what I've tried

我想从我的主方法调用isATens方法,但我只能在isATens没有参数的情况下这样做。我试着在调用者中输入相同的参数,但这似乎也无法识别

public class P1L4 {

    public static void main(String[] args) {
        P1L4 main = new P1L4();
        main.run();
        isATens(userInput); //<--- this is what I've tried doing.
    }

    public void run() {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Name a tens and i'll test if it's one under 100.");
        int userInput = scanner.nextInt();
    }

    public boolean isATens(int userInput) {
        System.out.println(userInput);
        switch (userInput) {
            case 10 : case 20 : case 30 : case 40 : case 50 : case 60: case 70: case 80: case 90 :
                isUnderOneHundred(continued);
            default :
                System.out.println("Not under one hundred");
        }
        return true;
    }

    public boolean isUnderOneHundred(int continued) {
        return true;
    }
}
公共类P1L4{
公共静态void main(字符串[]args){
P1L4 main=新的P1L4();
main.run();

isATens(userInput);//有些Java概念显然你还没有学会:作用域和实例与静态方法。如果你在理解我下面的评论时有困难,请阅读Java教科书中相应的章节

int userInput=scanner.nextInt();
run()
方法的作用域内声明,因此在
main()
方法中不可见。如果您想在run()方法之外看到
userInput
,我会将其设为该方法的返回值:

public int run() {
    ...
    int userInput = scanner.nextInt();
    return userInput;
}
您正在混合实例和静态方法,但没有任何可见的概念何时使用哪种方法。当您想从静态方法调用实例方法时,您需要在点之前命名实例,因此至少它必须是
main.isATens(userInput);
而不是
isATens(userInput);
(解决
userInput
问题后)


您的程序逻辑很奇怪,例如,如果参数小于100,我希望像
IsUnderOne100(int continued)
这样的方法返回true,但该方法甚至没有查看其参数,并且对于传入的任何数字都返回true。

userInput不是变量,也不是值。请尝试:isATens(5)或isATens()。此外,由于isATens不是静态方法,因此需要通过类的实例调用它。此外,还必须调用main.isATens(5)。而isunderOne方法是无意义的。在
run
method:
isATens(userInput)
扫描器
读取之后。@Stultuske userInput怎么不是一个变量?它不是用int userInput=Scanner.nextInt()声明的吗?在run方法中是局部的,但是你试图在main方法中使用它,它是未知的