Java中的Scanner来自第二个方法

huangapple go评论72阅读模式
英文:

Java Scanner from second method

问题

import java.util.Scanner;

public class Main {

    static void checkAge() {
        Scanner new_age = new Scanner(System.in);
        int age = new_age.nextInt();
        System.out.println("Enter your age");
        if (age < 18) {
            System.out.println("You are a minor");
        } else {
            System.out.println("You are of appropriate age");
        }
    }

    public static void main(String[] args) {
        checkAge();
    }
}
英文:

I am having problem to invoke Scanner class from second method in main method. My code is this:

import java.util.Scanner;

public class Main{
    
    static void checkAge(int age){
        Scanner new_age = new Scanner(System.in);
        age = new_age.nextInt();
        System.out.println(&quot;Enter your age&quot;);
        if(age &lt; 18){
            System.out.println(&quot;You are a minor&quot;);
        } else {
            System.out.println(&quot;You are of apropriate age&quot;);
        
        }
    }
    
    public static void main(String[] args){
        checkAge();
    }
}

I get an error:

> Main.java:18: error: '.class' expected
> checkAge(Scanner.new_age(int));
> ^ 1 error

答案1

得分: 0

调用函数并传递参数:checkAge(18); 或 checkAge(15); 因为checkAge()需要参数来检查条件。

英文:

Call you function with arguments : checkAge(18); or checkAge(15);as you checkAge() requires parameters to check the conditions.

huangapple
  • 本文由 发表于 2020年8月7日 13:43:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/63295874.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定