Java 使用方法将数据添加到超类和数组
我有一个Java应用程序,其中包含4个Java类 菜单,借书簿(超类),虚构书(扩展借书簿),非虚构书(扩展借书簿) 我被一种方法困住了——这种方法的目的是从我的阵列中向一名学生分发一本书。我的目标是寻找一本书,并在书中添加学生的详细信息。我想不出哪里出了问题,我就是想不通这个方法 请忽略任何额外的变量,但考虑到所有其他因素,如果需要更多信息,任何建议都将非常有用 我的问题是让别人看一下,告诉我什么是不需要的,我哪里做错了。有没有更简单的方法 PS我已经更改了下面的原始代码,但是在添加了名称后,我得到了一个运行时错误Java 使用方法将数据添加到超类和数组,java,arrays,superclass,subclassing,Java,Arrays,Superclass,Subclassing,我有一个Java应用程序,其中包含4个Java类 菜单,借书簿(超类),虚构书(扩展借书簿),非虚构书(扩展借书簿) 我被一种方法困住了——这种方法的目的是从我的阵列中向一名学生分发一本书。我的目标是寻找一本书,并在书中添加学生的详细信息。我想不出哪里出了问题,我就是想不通这个方法 请忽略任何额外的变量,但考虑到所有其他因素,如果需要更多信息,任何建议都将非常有用 我的问题是让别人看一下,告诉我什么是不需要的,我哪里做错了。有没有更简单的方法 PS我已经更改了下面的原始代码,但是在添加了名称后,
static void issueBook() {
int choice,x = 0;
String title,name,date;
boolean found = false;
System.out.println("1.Fiction or 2.NonFiction?");
choice = keyboard.nextInt();
if (choice == 1){
System.out.println("Please enter the title of the book to loan:");
title = keyboard.next();
System.out.println("Please enter your name:");
name = keyboard.next();
date = "todays date";
do {
if (fictionBooksArray[x].getTitle().equals(title)){
found = true;
fictionBooksArray[x].setOnLoan(true);
fictionBooksArray[location].setName(name);
fictionBooksArray[location].setDate(date);
System.out.println("Loan successful ");
}
else x++;
}
while ((x < fictionBooksArray.length)&& (!found));
if (!found){
System.out.println("This book title could not be located.");
}
}
static void发行本(){
整数选择,x=0;
字符串标题、名称、日期;
布尔值=false;
System.out.println(“1.小说还是2.非小说?”);
choice=keyboard.nextInt();
如果(选项==1){
System.out.println(“请输入借阅书籍的标题:”;
title=键盘.next();
System.out.println(“请输入您的姓名:”);
name=键盘.next();
日期=“今天日期”;
做{
if(数组[x].getTitle().equals(标题)){
发现=真;
数组[x].setOnLoan(true);
数组[location].setName(名称);
数组[location].setDate(日期);
System.out.println(“贷款成功”);
}
else x++;
}
而((x
这看起来像是家庭作业,所以我不会发布如何做到这一点,但一些通用的方法结构可能是这样的:
// Returns the book if it exists in books, otherwise returns null
static LoanBook find(LoanBook[] books, String title)
// Prints the prompt to the console, returns whatever the user types next
static String getUserInput(String prompt)
// Takes the input title and name, tries to find the book in the array
// If it detects the find method has failed by returning null, it prompts
// the user for new information
static void takeOutBook(LoanBook[] books)
// The big method is much clearer now, this depends on the other methods to work
static void issueBook() {
int choice = Integer.parseInt(getUserInput("1. Fiction: 2. Non Fiction:"));
if (choice == 1) {
takeOutBook(fictionBooksArray);
} else if (choice == 0) {
takeOutBook(nonfictionBookArray);
}
}
根据要求编辑完整示例:
隐含代码:
static class LoanBook {
String getTitle() { return ""; }
boolean isOnLoan() { return true; }
void setOnLoan(boolean loan) { }
void setDate(String d){ }
void setName(String d){ }
}
static class FictionBook extends LoanBook { }
static class NonFictionBook extends LoanBook { }
static Scanner keyboard = new Scanner(System.in);
static FictionBook[] fictionBooksArray = new FictionBook[10];
static NonFictionBook[] nonfictionBookArray = new NonFictionBook[10];
示例代码:
static void issueBook() {
int choice = Integer.parseInt(getUserInput("1. Fiction: 2. Non Fiction:"));
if (choice == 1) {
takeOutBook(fictionBooksArray);
} else if (choice == 0) {
takeOutBook(nonfictionBookArray);
}
}
static LoanBook find(LoanBook[] books, String title) {
for (LoanBook book : books) {
if (book.getTitle().equals(title) && !book.isOnLoan()) {
return book; // When the book is found, exit the loop and return the book
}
}
return null; // Returns null if book not found
}
static String getUserInput(String prompt) {
System.out.println(prompt);
return keyboard.next(); // You can then use Integer.parseInt(String param) to get the int value
}
static void takeOutBook(LoanBook[] books) {
String title = getUserInput("Please enter title of the book to loan");
String name = getUserInput("Please enter your name: ");
String date = "???";
LoanBook book = find(fictionBooksArray, title);
if (book != null) {
book.setOnLoan(true);
book.setName(name);
book.setDate(date);
} else {
System.out.println("The title has not been found, please try again");
takeOutBook(books); // The method calls itself in an loop until the user inserts valid information
}
}
实际问题是什么?尝试重构成一个方法,该方法采用LoanBook[]和一个标题,并返回匹配数组中的书籍。这应该会使它更可读。嗨,dave,我更新了这个问题。User60561,我不确定你的意思是什么
公共静态LoanBook find(LoanBook[]书籍,字符串标题)
。返回尚未借出的书名为的书籍。if语句的两端看起来相同,因此这将减少重复并使推理更容易。从描述中不清楚问题出在哪里,方法是在哪个类中…但很清楚,在else分支中增加索引是错误的。您对“for”循环和“do”循环使用相同的变量,而“one”则使用相同的变量。实际上,我不理解“for”的用途循环。这是家庭作业,但只是一个较大程序的一小部分,包括编写书籍、添加书籍、加载文件、编写文件、搜索排序、仅显示书籍等,因此更准确的答案会有所帮助。老实说,我认为问题出在我的初始数组。我更改了主要问题中的代码,请看locat在哪里来自堆栈跟踪的ion
以及setName方法的源代码也可能有帮助。