using scanner in java error: variable age might not have been initialized

using scanner in java error: variable age might not have been initialized

import java.io.*;
import java.util.Scanner;

public class Age {

public static void main(String []args) {

System.out.println("Enter Your Age: ");

int age;
Scanner in = new Scanner(System.in);
in.nextInt();

if(age<18)
{
   System.out.println("You Can't Vote");
}
else
{
    System.out.println("You Can Vote Now");
}
} 
}

编译器在编译过程中出错,请解决任何一个问题。

错误:变量 age 可能尚未初始化

编译器抱怨您没有初始化 age 并且您正在进一步使用它(在 if 条件下进行比较)。所以代替:

in.nextInt();

使用

age = in.nextInt();

age变量未初始化,初始化方式为:

年龄=in.nextInt();

import java.io.*;
import java.util.Scanner;

public class Age {

public static void main(String []args) {
int age;

System.out.println("Enter Your Age: ");

Scanner in = new Scanner(System.in);
age = in.nextInt();

if(age<18)
{
   System.out.println("You Can't Vote");
}
else
{
    System.out.println("You Can Vote Now");
}
} 
}