英文:
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("Enter your age");
if(age < 18){
System.out.println("You are a minor");
} else {
System.out.println("You are of apropriate age");
}
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论