0

大家好,我目前正在创建一个程序,该程序允许用户创建数组、搜索数组并从数组中删除元素。查看该LibraryMenu方法,在 switch 语句中创建数组的第一种情况工作正常,但是当我尝试编译时,其他情况会创建“找不到符号错误”。

我的问题是我希望搜索和删除函数引用第一个开关案例 - 创建库数组。任何帮助表示赞赏,即使它可能来自一个简单的错误。

import java.util.*;
public class EnterLibrary
{

public static void LibraryMenu()
    {
        java.util.Scanner scannerObject =new java.util.Scanner(System.in);
        LibraryMenu Menu = new LibraryMenu();
        Menu.displayMenu();
        switch (scannerObject.nextInt() )
        {
            case '1':
            {
                System.out.println ("1 - Add Videos");
                Library[] newLibrary;
                newLibrary = createLibrary();
            }
            break;
            case '2':
                System.out.println ("2 - Search Videos");
                searchLibrary(newLibrary);
                break;
            case '3':
            {
                System.out.println ("3 - Change Videos");
                    //Change video method TBA
            }
            break;      
            case '4':
                System.out.println ("4 - Delete Videos");
                deleteVideo(newLibrary);
                break;
            default:
                System.out.println ("Unrecognized option - please select options 1-3 ");
                break;
        }
    }

public static Library[] createLibrary()
{
    Library[] videos = new Library[4];
    java.util.Scanner scannerObject =new java.util.Scanner(System.in); 
    for (int i = 0; i < videos.length; i++)
    {
        //User enters values into set methods in Library class
        System.out.print("Enter video number: " + (i+1) + "\n");
        String number = scannerObject.nextLine();
        System.out.print("Enter video title: " + (i+1) + "\n");
        String title = scannerObject.nextLine();
        System.out.print("Enter video publisher: " + (i+1) + "\n");
        String publisher = scannerObject.nextLine();
        System.out.print("Enter video duration: " + (i+1) + "\n");
        String duration = scannerObject.nextLine();
        System.out.print("Enter video date: " + (i+1) + "\n");
        String date= scannerObject.nextLine();
        System.out.print("VIDEO " + (i+1) + " ENTRY ADDED " + "\n \n");
        //Initialize arrays
        videos[i] = new Library ();
        videos[i].setVideo( number, title, publisher, duration, date  );
    }
    return videos;
}

public static void printVidLibrary( Library[] videos)
{
    //Get methods to print results
    System.out.print("\n======VIDEO CATALOGUE====== \n");
    for (int i = 0; i < videos.length; i++)
    {
        System.out.print("Video number " + (i+1) + ": \n" + videos[i].getNumber() + "\n ");
        System.out.print("Video title " + (i+1) + ": \n" + videos[i].getTitle() + "\n ");
        System.out.print("Video publisher " + (i+1) + ": \n" + videos[i].getPublisher() + "\n ");
        System.out.print("Video duration " + (i+1) + ": \n" + videos[i].getDuration() + "\n ");
        System.out.print("Video date " + (i+1) + ": \n" + videos[i].getDate() + "\n ");
    }
}

public static Library searchLibrary( Library[] videos)
{
    //User enters values to setSearch
    Library titleResult = new Library();
    java.util.Scanner scannerObject =new java.util.Scanner(System.in); 
    for (int n = 0; n < videos.length; n++)
    {
        System.out.println("Search for video number:\n");
        String newSearch = scannerObject.nextLine();
        titleResult.getSearch( videos, newSearch);

        if (!titleResult.equals(-1))
        {
        System.out.print("Match found!\n" + newSearch + "\n");
        }
        else if (titleResult.equals(-1))
        {
        System.out.print("Sorry, no matches found!\n");
        }
    }
    return titleResult;
}

public static void deleteVideo( Library[] videos)
{
    Library titleResult = new Library();
    java.util.Scanner scannerObject =new java.util.Scanner(System.in); 
    for (int n = 0; n < videos.length; n++)
    {
        System.out.println("Search for video number:\n");
        String deleteSearch = scannerObject.nextLine();
        titleResult.deleteVideo(videos, deleteSearch);
        System.out.print("Video deleted\n");
    }
}



public static void main(String[] args)
{
    Library[] newLibrary;

    new LibraryMenu();
}
}
4

4 回答 4

2

我认为这是一个糟糕的设计。你把太多东西混在一起了:用户界面、逻辑、数据结构。

首先将您LibraryArrayLibraryMenu. 您根本不应该在其中看到任何开关或输入或输出。

Java 是一种面向对象的语言。开始从对象的角度考虑您的系统。我没有看到类似Videoand的类VideoCatalog。如果你创建了这个系统,你会发现它更容易实现。

看起来你有一个开始:

package model;

public class Video {
    private Long id;
    private String title;
    private String publisher;
    private int durationSeconds;
    private Date publicationDate;
    // add ctors, getters, etc.  Immutable?  Could be...
    // equals, hash code, toString
}

让您的 VideoCatalog 没有用户界面或 I/O:

package model;

public interface VideoCatalog {
    List<Video> find();
    List<Video> find(String title);
    List<Video> find(Date startDate, Date endDate) ;
    Long save(Video video);
    void update(Video video);
    void delete(Video video);
}

现在你可以有一个使用你想要的任何数据结构的实现:

package model;

public class VideoCatalogImpl implements VideoCatalog {
    private Set<Video> videos; 
    // add implementations here.
}
于 2012-06-10T16:30:20.140 回答
1

您需要将该数组变量的声明移出 first 的范围case,并移至其他情况可以看到它的某个地方。鉴于您的代码的当前结构,最方便的是使其成为类的静态成员 - 即,

public class EnterLibrary
{
    Library[] newLibrary;

那么这个类的所有静态方法都可以共享一个变量。但请务必删除出现在其他方法中的所有其他变量声明,否则它们仍将使用单独的变量,并且此类错误可能很难追踪!

于 2012-06-10T16:29:52.550 回答
0

试试这个,

声明Library[] newLibrary; as an instance variable (at class scope),或as local variable before the switch statement

于 2012-06-10T16:30:49.880 回答
0

Library[] newLibrary;仅在您的案例“1”中定义,您应该在更广泛的范围内定义它,例如您的LibraryMenu方法。此外,Library[] newLibrary您的 main 中声明的内容不会在任何地方调用,也许您应该在搜索中添加 Null 检查,打印删除方法。

您的构造函数类必须与您的类具有相同的名称,并且其中没有任何修饰符关键字。此外,当您创建类的对象时,它不会使用其中声明的静态方法。

注意:当您使用自己声明的数组时,最好声明一个int变量来跟踪数组的实际大小。请注意,它array.length返回数组可以有多少项目,而不是它已经有多少项目。

我会将您的定义(而不是代码)重新设计为如下所示:

//Note I changed the classname from EnterLibrary to LibraryMenu. Apparently you
//wanted a LibraryMenu class.
public class LibraryMenu {

    private final int MAX_ITEMS = 50;
    private Library[] videos;
    private int size = 0;

    //remove the static and void keyworkds from this method, so this will be 
    //the constructor.
    public LibraryMenu() {
        videos = new Library[MAX_ITEMS];
        //the rest of your code here...
        switch (scannerObject.nextInt()) {
        //if you're reading an int, keep the constants in the case as int.
        case 1: 
            //no need of brackets inside a case statement
            //check that you can add an item in your Library array
            //also, its not good to ask the user to add 4 (or N) videos in 1 round :).
            if (size < MAX_ITEMS) {
                Library video = addVideo();
                videos[size++] = video;
            }
            break;
        case 2:
            break;
        }
    }

    //remove the static keyword so the instance of your class can call the method.
    public Library addVideo() {
        Library video = new Library();
        //your code to read data goes here...
        //then fulfill the video and return it.
        return video;
    }

    //The Library[] videos is declared in your class, so all other methods could
    //use it without having to receive it as a parameter.
    public void printVidLibrary() {
        //your code goes here...
    }

    public Library searchLibrary() {
        //your code goes here...
    }

    public void deleteVideo( Library[] videos) {
        //your code goes here...
    }

    public static void main(String[] args) {
        new LibraryMenu();
    }
}
于 2012-06-10T16:46:06.380 回答