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变量未初始化,初始化方法为:
年龄 = 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");
}
}
}
编译器抱怨您尚未初始化年龄并且您正在进一步使用它(在 if 条件中进行比较)。所以代替:
in.nextInt();
使用
age = in.nextInt();