首页 > 解决方案 > 我在部门构造函数中的 if else in dept 函数中有错误

问题描述

我在 dept() 方法的 if else 语句中的 Department 构造函数中有错误。我该如何解决这个问题?

我尝试了几次尝试,通过使用不同的函数名称使函数抽象,但它不起作用。

课本

import java.util.*; 

class Book
{
    int Book_id;
    String Book_Name;
    String Author_Name;
    int pages;
    float prices;

    void getbook()
    {
        Scanner SC=new Scanner(System.in);
        System.out.println("Enter the Book ID");
        Book_id=SC.nextInt();
        System.out.println("Enter the Book Name");
        Book_Name=SC.next();
        System.out.println("Enter the Author Name");
        Author_Name=SC.next();
        System.out.println("Enter the pages");
        pages=SC.nextInt();
        System.out.println("Enter the prices");
        prices=SC.nextFloat();
    }

    void display()
    {
        System.out.println("Book Id"+Book_id);
        System.out.println("Book Name"+Book_Name);
        System.out.println("Author Name"+Author_Name);
        System.out.println("Pages"+pages);
        System.out.println("Prices"+prices);
    }
}

班级学生

class Student extends Book
{
    String Student_N;
    int roll_no;
}

班级部

class Department extends Student 
{
    int choice;
    String Dept_Code;
    Department(int roll,String C)
    {

        System.out.println(""+C+""+roll_no);
    }

    void  dept()
    {
        Scanner SC=new Scanner(System.in);
        System.out.println("1.Civil Enginerring");
        System.out.println("2.Computer Enginerring");
        System.out.println("3.Information Tecnology");
        System.out.println("4.Mechanical Engineering");
        System.out.println("5.Electorics Enginerring");
        System.out.println("6.Electrical Enginerring");
        System.out.println("Enter the choice");
        choice=SC.nextInt();
        switch (choice)
            {
            case 1:
                dept_data(); 
                break;
            case 2:
                dept_data();
                break;
            case 3:
                dept_data();
                break;
            case 4:
                dept_data();
                break;
            case 5:
                dept_data();
                break;
            case 6:
                dept_data();
                break;

        }
    }

    void dept_data()
    {
        Scanner SC=new Scanner(System.in);
        System.out.println("Enter the Student Details");
        System.out.println("Enter the Student Name");
        SC.next();
        System.out.println("Enter the Student Roll No");
        SC.nextInt();
        System.out.println("Enter the Book ID");
        SC.nextInt();
        System.out.println("Enter the Book Name");
        SC.next();
    }

    int dept_del(String C)
    {
        Scanner SC=new Scanner(System.in);
        if(C.compareTo("ci")==0||C.compareTo("CI")==0)
        {
            System.out.println("Enter the roll no");
            roll_no=SC.nextInt();
            Department(roll_no,C); //error
        }
        else if(C.compareTo("IT")==0||C.compareTo("it")==0)
        {
            System.out.println("Enter the roll no");
            roll_no=SC.nextInt();
            Department(roll_no,C); //error
        }
        else if(C.compareTo("CO")==0||C.compareTo("co")==0)
        {
            System.out.println("Enter the roll no");
            roll_no=SC.nextInt();
            Department(roll_no,C); //error
        }
        else if(C.compareTo("ME")==0||C.compareTo("me")==0)
        {
            System.out.println("Enter the roll no");
            roll_no=SC.nextInt();
            Department(roll_no,C); //error
        }
        else if(C.compareTo("EC")==0||C.compareTo("ec")==0)
        {
            System.out.println("Enter the roll no");
            roll_no=SC.nextInt();
            Department(roll_no,C); //error
        }
        else if(C.compareTo("EE")==0||C.compareTo("ee")==0)
        {
            System.out.println("Enter the roll no");
            roll_no=SC.nextInt();
            Department(roll_no,C); //error
        }
    }

}

类项目

class Project 
{
    public static void main(String[] args)
    {
        int i=1,flag=0;
        Department A[]=new Department[100];
        Scanner SC=new Scanner(System.in);
        System.out.println("Enter the Choice");
        System.out.println("1.Add a Book\n2.Display the Book");
        while(flag==0)
        {
            int choice=SC.nextInt();
            switch (choice)
            {
                case 1 :                    
                        A[i].getbook(); 
                        i++;                        
                case 2:
                        int n=i;
                        for(i=0;i<n;i++)
                          A[i].display();

                case 3:
                        flag=1;
                        break;
                default:
                        System.out.println("Wrong Choice");
            }

        }

    }
}

标签: java

解决方案


构造函数中的问题是它接收int roll, String C它们并且不处理它们,并且在构造函数执行后未定义实例变量int choice& 。String Dept_Code

也许你想做:

Department(int choice, String Dept_Code) {
    this.choice = choice;
    this.Dept_Code = Dept_Code;
    System.out.println("" + Dept_Code + " - " + choice);
}

switch(choice)案例 1 和案例 2 之后,如果您想单独添加一本书在案例 1 和案例 3 中缺少一些休息时间:

switch(choice) {
    case 1 :
        A[i].getbook(); 
        i++;
        break;

    case 2:
        int n=i;
        for(i=0;i<n;i++)
          A[i].display();
        break;

    case 3:
        flag=1;
        break;

    default:
        System.out.println("Wrong Choice");
}

dept_data() 而在另一个 switch 中,如果所有 case 都执行相同的代码,那么创建 switch 是没有意义的。

并且作为建议,最好在比较时始终首先使用常量以防止可能的空指针: "ci".compareTo(C)C.compareTo("ci")如果 C 未定义,第二个选项可能会引发空指针异常要好。另外,看看命名约定:https ://www.geeksforgeeks.org/java-naming-conventions/


推荐阅读