我的教授拒绝告诉我我在做什么错,而我一生都无法解决。我刚开始使用Java,所以看起来可能很愚蠢,但是
import java.util.Scanner;
public class Library {
MediaItem[] items = new MediaItem[100];
int numberOfItems = 0;
static int displayMenu(){
@SuppressWarnings("resource")
Scanner dog = new Scanner(System.in);
System.out.println("Menu:\n1:Add New Item\n2:Mark Item as Loaned\n3:List All Items\n4:Mark Item as Returned\n5:Quit");
int cat = dog.nextInt();
return cat;
}
void addNewItem(String title, String format){
MediaItem item = new MediaItem();
item.setTitle(title);
item.setFormat(format);
items[numberOfItems] = item;
numberOfItems++;
}
void markItemOnLoan(String title, String name, String date){
for (int i=0;i<numberOfItems;i++){
MediaItem item = items[i];
String title5 = item.getTitle();
if (title5 == title){
item.markOnLoan(name, date);
i=numberOfItems;
}
}
}
String[] listAllItems(){
String[] all = new String[numberOfItems];
for (int i=0;i<numberOfItems;i++){
MediaItem item = items[i];
if (item.onLoan=true){
all[i]=item.getTitle()+" "+item.getFormat()+" "+item.getOnLoan()+" "+item.getLoanedTo()+" "+item.getDateLoaned()+" ";
}else{
all[i]=item.getTitle()+" "+item.getFormat()+" "+item.getOnLoan()+" ";
}
}
return all;
}
void markItemReturned(String title){
for (int i=0;i<numberOfItems;i++){
MediaItem item = items[i];
String title1 = item.getTitle();
if (i==numberOfItems){
System.out.println("Title not found.");
}else if (title1 == title){
item.markReturned();
i=numberOfItems;
}
}
}
public static void main(String[] args) {
Library banana = new Library();
String invalid = "Invalid Option";
@SuppressWarnings("resource")
Scanner dog = new Scanner(System.in);
for (int i=0; i==0;){
int cat = displayMenu();
if (cat<=0){
System.out.println(invalid);
}
if (cat>=6){
System.out.println(invalid);
}
switch (cat){
case 1:
System.out.println("What is the title of the item?");
String title = dog.nextLine();
System.out.println("What is the format of the item?");
String format = dog.nextLine();
banana.addNewItem(title, format);
break;
case 2:
System.out.println("What is the title of the item?");
String title1 = dog.nextLine();
System.out.println("What is your name?");
String name = dog.nextLine();
System.out.println("What is the date?");
String date = dog.nextLine();
banana.markItemOnLoan(title1, name, date);
break;
case 3:
String[] list = banana.listAllItems();
System.out.println("List of all items:\n");
for (int o=0;o<banana.numberOfItems;o++){
System.out.println(list[o]);
}
break;
case 4:
System.out.println("What is the title you are returning?");
String title2 = dog.nextLine();
banana.markItemReturned(title2);
break;
case 5:
i++;
break;
}
}
}
}
我在整个代码中都遇到了这个问题,但是我想如果你们能回答这个问题,我可以自己解决其余的问题。
编辑:我走得更远,现在我只需要弄清楚为什么它没有读取名称和日期。
最佳答案
在您的switch语句中,您调用addNewItem,该声明未声明为静态。
为了调用addNewItem,您必须有一个Object可以从中调用它,除非您将其声明为静态,或调用obj.addNewItem()
。您没有包含太多代码,所以我不确定对象应该是什么类。
static void addNewItem(String title, String format){
MediaItem item = new MediaItem();
item.setTitle(title);
item.setFormat(format);
items[numberOfItems] = item;
numberOfItems++;
}
static
关键字表示即使没有与之关联的类的实例,方法,字段或类也只有一个实例。如果没有static修饰符,则需要一个类的实例来访问方法,字段或类的来源。例如,请参见:https://stackoverflow.com/a/15828090/1732480
编辑:发布更多代码后,您没有Library实例可以调用addNewItem。
用
Library l = new Library();
创建一个库并调用l.addNewItem()
public static void main(String[] args) {
Library l = new Library();
String invalid = "Invalid Option";
Scanner dog = new Scanner(System.in);
for (int i=0; i==0;){
int cat = displayMenu();
if (cat<=0){
System.out.println(invalid);
}
if (cat>=6){
System.out.println(invalid);
}
switch (cat){
case 1:
System.out.println("What is the title of the item?");
String title = dog.nextLine();
System.out.println("What is the format of the item?");
String format = dog.nextLine();
l.addNewItem(title, format);
break;
....