Java:包含涉及数组的方法的Switch语句

Java:包含涉及数组的方法的Switch语句,java,arrays,switch-statement,Java,Arrays,Switch Statement,大家好,我现在正在创建一个程序,允许用户创建一个数组,搜索一个数组并从数组中删除一个元素。查看LibraryMenu方法,在switch语句中创建数组的第一种情况可以正常工作,但是在我尝试编译时,其他情况会创建“找不到符号错误” 我的问题是,我希望搜索和删除函数引用第一个开关案例-创建库数组。感谢您的帮助,即使可能是因为一个简单的错误 import java.util.*; public class EnterLibrary { public static void LibraryMenu()

大家好,我现在正在创建一个程序,允许用户创建一个数组,搜索一个数组并从数组中删除一个元素。查看
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();
}
}
import java.util.*;
公共图书馆
{
公共静态无效库菜单()
{
java.util.Scanner scannerObject=新的java.util.Scanner(System.in);
LibraryMenu=新建LibraryMenu();
Menu.displayMenu();
开关(scannerObject.nextInt())
{
案例“1”:
{
System.out.println(“1-添加视频”);
图书馆[]新图书馆;
newLibrary=createLibrary();
}
打破
案例“2”:
System.out.println(“2-搜索视频”);
搜索图书馆(新图书馆);
打破
案例“3”:
{
System.out.println(“3-更改视频”);
//更改视频方法TBA
}
打破
案例“4”:
System.out.println(“4-删除视频”);
删除视频(新图书馆);
打破
违约:
System.out.println(“无法识别的选项-请选择选项1-3”);
打破
}
}
公共静态库[]createLibrary()
{
库[]视频=新库[4];
java.util.Scanner scannerObject=新的java.util.Scanner(System.in);
对于(int i=0;i
您需要将该数组变量的声明移出第一个
案例的范围,并移动到其他案例可以看到它的地方。考虑到代码的当前结构,将其设置为类的静态成员是最方便的,即


然后这个类的所有静态方法都可以共享一个变量。但是一定要删除其他方法中出现的所有其他变量声明,否则它们仍然会使用单独的变量,这样的错误可能很难找到

您需要将该数组变量的声明移出第一个
案例的范围,并移动到其他案例可以看到它的地方。考虑到代码的当前结构,将其设置为类的静态成员是最方便的,即


然后这个类的所有静态方法都可以共享一个变量。但是一定要删除其他方法中出现的所有其他变量声明,否则它们仍然会使用单独的变量,这样的错误可能很难找到

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

从隔离
LibraryArray
fr开始
public class EnterLibrary
{
    Library[] newLibrary;
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
}
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.
}
//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();
    }
}