永远不会读取局部变量名称?我该怎么办?

时间:2014-02-07 00:40:22

标签: java

它有一个注释,永远不会读取本地变量名称。请帮助我差不多完成它:)感谢您的帮助。我正在使用Dr.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);



}

4 个答案:

答案 0 :(得分:0)

唯一的错误是声明

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);

应该在main方法内,而不是在类块

答案 1 :(得分:0)

println语句在main之外。代码应如此:

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();

        if(!(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 age(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);

        input.close();
    }

}

你的while循环(年级)也有逻辑错误......

答案 2 :(得分:-1)

它必须位于name变量上。

你已经分配了一些东西。但是在给它赋值之后你还没有在其他任何地方使用它。由于它未被使用,IDE的只是通知你没有使用它。

另一个主要原因是您在定义print变量的方法中有name个语句。将它们移到方法中,然后警告就会出现。

更改:

    }

    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);

}

致:

        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);

    } // main
} // class

答案 3 :(得分:-4)

真正的问题是你为什么设置变量但从不使用它的值。如果你真的不需要它,只需删除它。如果你确实需要它,显然你已经遗漏了你的程序。