Java 即使参数正确,也找不到构造函数
我一直在试图找出这个错误的原因,但我无法:(我需要你的帮助来解决它:) 1.我检查了实际的参数列表,以确保它与正式的参数列表匹配,它确实匹配 2.当我将其替换为当前值时,没有参数列表的构造函数Book()工作正常 错误:Java 即使参数正确,也找不到构造函数,java,object,constructor,compiler-errors,Java,Object,Constructor,Compiler Errors,我一直在试图找出这个错误的原因,但我无法:(我需要你的帮助来解决它:) 1.我检查了实际的参数列表,以确保它与正式的参数列表匹配,它确实匹配 2.当我将其替换为当前值时,没有参数列表的构造函数Book()工作正常 错误: Library.java:83: cannot find symbol symbol : constructor Book(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String) loc
Library.java:83: cannot find symbol
symbol : constructor Book(int,java.lang.String,java.lang.String,java.lang.String,java.lang.String)
location: class Book
libraryBooks[100-Book.totalNum] = new Book (ISBN,Author,publishedYear,title,genre);
^
1 error
教材:
public class Book {
private int ISBN;
private String author;
private String publicationYear;
private String title;
private String genre;
public static int totalNum=1;
private String refCode;
public void Book(int isbn,String auth,String year,String Title,String Genre){
ISBN=isbn;
author=auth;
publicationYear=year;
title=Title;
genre=Genre;
}
public void Book(){
ISBN=1234;
author="-";
publicationYear="2012";
title="-";
genre="-";
}
public static boolean verifyISBN(int isbn1){
int[] n=new int [4];
int isbn=isbn1;
n[3]=isbn%10;
for(int i=2;i>=0;i--){
isbn/=10;
n[i]=isbn%10;}
if (((n[0]*3+n[1]*2+n[2]*1)%4)==n[3])
return true;
return false;
}
}
类库:
import java.util.*;
public class Library {
private static Scanner console=new Scanner(System.in);
private static Book [] libraryBooks=new Book [100];
public static void main (String[]args){
System.out.println("Enter the book\'s ISBN");
int ISBN=console.nextInt();
System.out.println("Enter the book\'s author name");
String author=console.next();
System.out.println("Enter the book\'s publish year");
String publicationYear=console.next();
System.out.println("Enter the book\'s title");
String title=console.next();
System.out.println("Enter the book\'s genre");
String genre=console.next();
if(addBook(ISBN,author,publicationYear,title,genre))
System.out.println("The book was successfully added to your library");
else
System.out.println("The book sasn't successfully added to your library");
}
public static boolean addBook(int ISBN,String Author,String publishedYear,String title, String genre){
if (Book.verifyISBN(ISBN)&&(findBook(ISBN)!=-1)){
libraryBooks[100-Book.totalNum] = new Book (ISBN,Author,publishedYear,title,genre);
Book.totalNum++;
return true;}
return false;
}
public static int findBook(int ISBN){
for (int i=99;i>=(100-Book.totalNum);i--)
if(libraryBooks[i].getISBN()==ISBN)
return i;
return -1;
}}
请帮助我:(!由于
void
关键字(构造函数没有返回类型),此行不是构造函数。您创建了一个返回void
的方法
public void Book(int isbn,String auth,String year,String Title,String Genre){
删除void
:
public Book(int isbn,String auth,String year,String Title,String Genre){
您需要对您的另一本书
“方法”进行类似操作:
您没有定义任何实际的构造函数,因此Java隐式地包含了一个默认构造函数,它不带参数(也不做任何事情)。这就是为什么不带参数调用它不会出错的原因。由于
void
关键字(构造函数没有返回类型),此行不是构造函数。您已经创建了一个返回void
的方法
public void Book(int isbn,String auth,String year,String Title,String Genre){
删除void
:
public Book(int isbn,String auth,String year,String Title,String Genre){
您需要对您的另一本书
“方法”进行类似操作:
您没有定义任何实际的构造函数,因此Java隐式地包含了一个默认构造函数,它不带参数(也不做任何事情)。这就是为什么不带参数调用它不会出错的原因。由于
void
关键字(构造函数没有返回类型),此行不是构造函数。您已经创建了一个返回void
的方法
public void Book(int isbn,String auth,String year,String Title,String Genre){
删除void
:
public Book(int isbn,String auth,String year,String Title,String Genre){
您需要对您的另一本书
“方法”进行类似操作:
您没有定义任何实际的构造函数,因此Java隐式地包含了一个默认构造函数,它不带参数(也不做任何事情)。这就是为什么不带参数调用它不会出错的原因。由于
void
关键字(构造函数没有返回类型),此行不是构造函数。您已经创建了一个返回void
的方法
public void Book(int isbn,String auth,String year,String Title,String Genre){
删除void
:
public Book(int isbn,String auth,String year,String Title,String Genre){
您需要对您的另一本书
“方法”进行类似操作:
您没有定义任何实际的构造函数,因此Java隐式包含了一个默认构造函数,它不带参数(也不做任何事情)。这就是为什么不带参数调用它不会出错的原因。在这个程序中,您会犯一个很小的错误。 您忘记了构造函数的一个重要属性,即构造函数没有返回类型。 在这里,您将返回类型指定为void,但其内部自动返回当前类对象。
我希望你现在能更清楚地了解这个概念。在这个节目中,你犯了一个很小的错误。 您忘记了构造函数的一个重要属性,即构造函数没有返回类型。 在这里,您将返回类型指定为void,但其内部自动返回当前类对象。
我希望你现在能更清楚地了解这个概念。在这个节目中,你犯了一个很小的错误。 您忘记了构造函数的一个重要属性,即构造函数没有返回类型。 在这里,您将返回类型指定为void,但其内部自动返回当前类对象。
我希望你现在能更清楚地了解这个概念。在这个节目中,你犯了一个很小的错误。 您忘记了构造函数的一个重要属性,即构造函数没有返回类型。 在这里,您将返回类型指定为void,但其内部自动返回当前类对象。
我希望现在这个概念会更清楚。
isbn
实际上应该是一个字符串
。isbn中的最后一个字符可以是数字0-9
或X
。惯例是变量名使用小写。将字符串标题
替换为最终字符串标题
,并使用作业语句中的this.title=title
。您的验证ISBN
算法也错误;请参阅。事实上,ISBN-10代码必须有10位数字。您是否使用较新的ISBN-13?ISBN
实际上应该是字符串
。但是,ISBN中的最后一个字符可能是数字0-9
或X
。惯例是变量名使用小写。将字符串标题
替换为最终字符串标题
,并在赋值语句中使用this.Title=Title
。您的验证ISBN
算法也错误;请参阅。事实上,ISBN-10代码必须有10位数字。您是否在中使用较新的ISBN-13然而,isbn实际上应该是一个字符串
。isbn中的最后一个字符可以是数字0-9
或X
。惯例是变量名使用小写。将字符串标题
替换为最终字符串标题
,并在赋值中使用this.Title=Title
t语句。您的验证ISBN
算法也是错误的;请参阅。事实上,ISBN-10代码必须有10位数字。您是否使用较新的ISBN-13?ISBN
实际上应该是字符串
。但是,ISBN中的最后一个字符可能是数字0-9
或X
。惯例是使用小写字母变量名称。将字符串标题
替换为最终字符串标题
,并在赋值语句中使用this.Title=Title
。您的验证ISBN
算法也错了;请参阅。事实上,ISBN-10代码必须有10位数字。您是否改用较新的ISBN-13?