gpt4 book ai didi

java - 无法对非静态方法库进行静态引用

转载 作者:太空宇宙 更新时间:2023-11-04 14:27:14 24 4
gpt4 key购买 nike

我的教授拒绝告诉我我做错了什么,而我一生都无法弄清楚。我刚刚开始学习 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,您必须有一个对象来调用它,除非您将其声明为静态,或者调用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

编辑:发布更多代码后,您没有可调用 addNewItem 的 Library 实例。使用 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;
....

关于java - 无法对非静态方法库进行静态引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26533615/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com