标题翻译
I had error in Department constructor in if else in dept function
问题
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 Engineering");
		System.out.println("2.Computer Engineering");
		System.out.println("3.Information Technology");
		System.out.println("4.Mechanical Engineering");
		System.out.println("5.Electronics Engineering");
		System.out.println("6.Electrical Engineering");
		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");
			}
                
		}
	}
}
英文翻译
I had an error in the Department constructor in the if else statement in dept() method. How can I solve this?
I tried several tries by making the function abstract using different function name but it doesn't work.
class Book
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
class Student extends Book
{
	String Student_N;
	int roll_no;
}
class Department
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
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");
			}
                
		}
	}
}
答案1
得分: 0
在您的构造函数中存在的问题是,它接收了 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) 中,如果您想要将“添加书籍”与“显示书籍”分开,并且在 case 1 和 2 后面缺少了一些 break:
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");
}
另一个 switch 中,如果所有的 case 都只执行相同的 dept_data() 代码,那么创建一个 switch 是没有意义的。
作为建议,始终在比较时首先使用常量,以防止可能的空指针:
"ci".compareTo(C) 比 C.compareTo("ci") 更好,因为第二种选项如果 C 未定义可能会抛出空指针异常。另外,请参考命名约定:https://www.geeksforgeeks.org/java-naming-conventions/。
英文翻译
The problem in your constructor is that it receives int roll, String C and does nothig with them and the instance vars int choice& String Dept_Codeare not defined after the constructor executes.
Maybe you wanted to do:
Department(int choice, String Dept_Code) {
    this.choice = choice;
    this.Dept_Code = Dept_Code;
    System.out.println("" + Dept_Code + " - " + choice);
}
In the switch(choice) some breaks are missing after case 1 & 2 if you want to separate add a book fom display the book and from case 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");
}
And in the other switch it makes no sense to create a switch if all the cases just execute the same dept_data()
code.
And as a suggestion it's better to always use the constant first when comparing to prevent from possible null pointers:
"ci".compareTo(C) is better than C.compareTo("ci") as the second option could throw a null pointer exception if C is undefined. Also, have a look at naming conventions: https://www.geeksforgeeks.org/java-naming-conventions/
专注分享java语言的经验与见解,让所有开发者获益!

评论