我在部门构造函数中的 if else 部分出现了错误,在 dept 函数中。

huangapple 未分类评论61阅读模式
标题翻译

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(&quot;Enter the Book ID&quot;);
		Book_id=SC.nextInt();
		System.out.println(&quot;Enter the Book Name&quot;);
		Book_Name=SC.next();
		System.out.println(&quot;Enter the Author Name&quot;);
		Author_Name=SC.next();
		System.out.println(&quot;Enter the pages&quot;);
		pages=SC.nextInt();
		System.out.println(&quot;Enter the prices&quot;);
		prices=SC.nextFloat();
	}
	
	void display()
	{
		System.out.println(&quot;Book Id&quot;+Book_id);
		System.out.println(&quot;Book Name&quot;+Book_Name);
		System.out.println(&quot;Author Name&quot;+Author_Name);
		System.out.println(&quot;Pages&quot;+pages);
		System.out.println(&quot;Prices&quot;+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(&quot;&quot;+C+&quot;&quot;+roll_no);
	}

    void  dept()
	{
		Scanner SC=new Scanner(System.in);
		System.out.println(&quot;1.Civil Enginerring&quot;);
		System.out.println(&quot;2.Computer Enginerring&quot;);
		System.out.println(&quot;3.Information Tecnology&quot;);
		System.out.println(&quot;4.Mechanical Engineering&quot;);
		System.out.println(&quot;5.Electorics Enginerring&quot;);
		System.out.println(&quot;6.Electrical Enginerring&quot;);
		System.out.println(&quot;Enter the choice&quot;);
		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(&quot;Enter the Student Details&quot;);
		System.out.println(&quot;Enter the Student Name&quot;);
		SC.next();
		System.out.println(&quot;Enter the Student Roll No&quot;);
		SC.nextInt();
		System.out.println(&quot;Enter the Book ID&quot;);
		SC.nextInt();
		System.out.println(&quot;Enter the Book Name&quot;);
		SC.next();
	}
	
	int dept_del(String C)
	{
		Scanner SC=new Scanner(System.in);
		if(C.compareTo(&quot;ci&quot;)==0||C.compareTo(&quot;CI&quot;)==0)
		{
			System.out.println(&quot;Enter the roll no&quot;);
			roll_no=SC.nextInt();
			Department(roll_no,C); //error
		}
		else if(C.compareTo(&quot;IT&quot;)==0||C.compareTo(&quot;it&quot;)==0)
		{
			System.out.println(&quot;Enter the roll no&quot;);
			roll_no=SC.nextInt();
			Department(roll_no,C); //error
		}
		else if(C.compareTo(&quot;CO&quot;)==0||C.compareTo(&quot;co&quot;)==0)
		{
			System.out.println(&quot;Enter the roll no&quot;);
			roll_no=SC.nextInt();
			Department(roll_no,C); //error
		}
		else if(C.compareTo(&quot;ME&quot;)==0||C.compareTo(&quot;me&quot;)==0)
		{
			System.out.println(&quot;Enter the roll no&quot;);
			roll_no=SC.nextInt();
			Department(roll_no,C); //error
		}
		else if(C.compareTo(&quot;EC&quot;)==0||C.compareTo(&quot;ec&quot;)==0)
		{
			System.out.println(&quot;Enter the roll no&quot;);
			roll_no=SC.nextInt();
			Department(roll_no,C); //error
		}
		else if(C.compareTo(&quot;EE&quot;)==0||C.compareTo(&quot;ee&quot;)==0)
		{
			System.out.println(&quot;Enter the roll no&quot;);
			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(&quot;Enter the Choice&quot;);
		System.out.println(&quot;1.Add a Book\n2.Display the Book&quot;);
		while(flag==0)
		{
			int choice=SC.nextInt();
			switch (choice)
			{
				case 1 :					
						A[i].getbook();	
						i++;						
				case 2:
						int n=i;
						for(i=0;i&lt;n;i++)
						  A[i].display();
						  
				case 3:
						flag=1;
						break;
				default:
						System.out.println(&quot;Wrong Choice&quot;);
			}
                
		}

	}
}

答案1

得分: 0

在您的构造函数中存在的问题是,它接收了 int roll, String C,却没有对它们做任何处理,而实例变量 int choiceString 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(&quot;&quot; + Dept_Code + &quot; - &quot; + 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&lt;n;i++)
          A[i].display();
        break;

    case 3:
        flag=1;
        break;

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

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:
&quot;ci&quot;.compareTo(C) is better than C.compareTo(&quot;ci&quot;) 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/

huangapple
  • 本文由 发表于 2020年3月16日 20:49:06
  • 转载请务必保留本文链接:https://java.coder-hub.com/60706328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定