java局部变量名从未被读取?我该怎么办?
它有一条注释,表示永远不会读取局部变量名。谢谢你的帮助。我用的是Java博士的IDE
import java.util.Scanner;
public class information {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String name ;
String course;
char gender;
int yearLevel;
int age;
System.out.print("Enter your name: ");
name = input.nextLine();
System.out.print("Enter your gender [M/F]: ");
gender = input.nextLine().charAt(0);
while(gender!= 'M' && gender!= 'F'){
System.out.println("Error: type only M or F");
System.out.println("Re-enter your gender [M/F]: ");
gender = input.nextLine().charAt(0);
}
System.out.print("Enter your course: "); // BSCS, BSIT or BSIS
course = input.nextLine();
while( course.equals("BSCS") || course.equals("BSIT") || course.equals("BSIS")){
System.out.println("Error: type only BSCS,BSIT, or BSIS");
System.out.println("Re-enter your course: ");
course = input.nextLine();
}
System.out.print("Enter your year level: ");/// 1 to 4
yearLevel = input.nextInt();
while((yearLevel<4 || yearLevel>1) )
{
System.out.print("you entered wrong year level(re-enter):");
yearLevel = input.nextInt();
}
System.out.print("Enter your age: ");
age = input.nextInt();
while((age>=16) )
{
System.out.print("you entered wrong year level(re-enter):");
yearLevel = input.nextInt();
}
}
System.out.println();
System.out.println("Student Information");
System.out.printf("%s (%c)\n", name, gender);
System.out.printf("%d years old\n", age);
System.out.println(course + "-" + yearLevel);
}
# 1 楼答案
唯一的错误是
应该在
main
方法中,而不是类块中# 2 楼答案
它必须位于
name
变量上你有分配给它的东西。但在为其赋值后,您没有在其他任何地方使用过它。由于它未被使用,IDE的只需通知您它未被使用
另一个主要原因是
print
语句来自定义name
变量的方法。将他们移动到方法中,警告就会消失改变:
致: