0

我有一些课程,例如UniversityDepartment&PolytechnicDepartment这两个课程扩展了AcademicDepartment课程。问题要我这样做。可以有AcademicDepartment研究生课程和研究生课程。部门将是 2 University& Polytechnic。一个部门是理工学院还是大学,取决于研究生课程中实验室的数量。因此,如果我们在预科或研究生课程中,我认为用户必须从键盘输入它,如果我们在预科,我们还必须被问到如果我们在理工学院或大学,我们有多少个实验室。那么我将如何做到这一点?我会给你一个我的代码示例:

import java.io.*;

public class  AcademicDepartment {
    private String names;
    private String labs;
    private int teachers;
    private int graduates;
    // private boolean studies;

    public void infostudies() throws IOException{
        System.out.println("Enter the department you want."
            + "Press 1 for pre-graduate program  or 2 for after-graduate program" );  
        String studies = "";  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        studies = br.readLine(); 

        if (studies == 1){
            System.out.println("You are in pre-graduate program");
            System.out.println("Enter the number of labs");
            String labs = ""; 
            BufferedReader br1 = new BufferedReader(new InputStreamReader(System.in));
            labs = br1.readLine();

            if (labs > 5){
                System.out.println("The department is polytechnic");
            }
        }        

        if (labs < 5 ){
            System.out.println("The department is University");
        }
        else
        {
            System.out.println("Wrong number try again");
        }
    }    

    public AcademicDepartment(String names,String labs,int teachers,int graduates){
        names = new String(names);
        this.labs = labs;
        this.teachers = teachers;
        this.graduates = graduates;
    }

    public void printdepartmentinfo(){
        System.out.println("The names are:" + names);
        System.out.println("The labs are:" + labs);
        System.out.println("The GRADUATES are:" + graduates);
        System.out.println("The teachers are:" + teachers);   
    }
}
4

1 回答 1

0

关于代码有一些不幸的事情。

一个问题是您正在比较字符串变量studieslabs整数文字。这是编译器不允许的。通常,要么将字符串变量(使用String.equals)与字符串文字进行比较,要么尝试Integer.parseInt在与整数进行比较之前使用解释字符串变量。

另一个问题是方法infostudies的一些局部变量与类的字段具有相同的名称AcademicDepartment。这是允许的,但这是不必要的混乱。

例如,您将处理 variable studies,但我将向您展示我对 variable 的含义labs

不要这样做:

public class AcademicDepartment {
    ...
    public void infostudies() throws IOException {
        ...
        if (studies == 1) {
            ...
            String labs = "";
            ...
            if (labs > 5){ // Compare String with int? Won't compile!
                ...
            }
            ...
        }
        if (labs < 5) { // Can't see the "labs" defined above!
            ...
        }
        ...
    }
}

做这个:

public class AcademicDepartment {
    ...
    public void infostudies() throws IOException {
        ...
        if (studies == 1) {
            ...
            String labs = "";
            ...
            try {
                   int labCount = Integer.parseInt(labs);
                   if (labCount > 5) {
                       System.out.println("The department is polytechnic");
                   } else {
                       System.out.println("The department is University");
                   }
            } catch (NumberFormatException formatErr) {
                System.out.println("Sorry, couldn't understand your number of labs.");
            }
            ...
        }
        ...
    }
}
于 2013-03-17T17:56:48.200 回答