繁体   English   中英

我无法理解为什么它显示为null?

[英]i can't undrestand why it displays null?

        //Main : 
        package DevAyo;

        import java.util.Scanner;

        public class Main {
         private static Scanner scanner =new Scanner(System.in);
         private static Album Alb =new Album("Kamikaze");
            public static void main(String[] args) {
              boolean quit = true;
              while (quit){
                System.out.println("Choose actions : ");
                System.out.println("\t\t 1.Add new Songs to Album :");
                System.out.println("\t\t 2.printSongs");
                System.out.println("\t\t 3.Quit");
                System.out.println("choose Action : ");
                int choice = scanner.nextInt();
                scanner.nextLine();
                switch (choice){
                    case 1:
                        AddSong();
                        break;
                    case 2:
                        Alb.printSongs();
                        break;
                    case 3:
                        quit=false;
                        break;
                }
            }
            }
            public static void AddSong(){
        //        System.out.println("Insert Album Name:  ");
        //        String albumName =scanner.nextLine();
                System.out.println("Pleas Enter Number of Songs :");
                int Number = scanner.nextInt();
                for (int i=0;i<Number;i++){
                    System.out.println("Enter Song Name "+(i+1)+": ");
                    String name=scanner.nextLine();
                    scanner.nextLine();
                    System.out.println("Enter Singer of Song "+(i+1)+": ");
                    String SingerName =scanner.nextLine();
                    System.out.println("Enter Song "+(i+1)+" released Day: ");
                    String ReleasedDay = scanner.nextLine();
                    System.out.println("Enter Length of the  Song"+(i+1)+": ");
                    int SongLength=scanner.nextInt();
                    Songs song = Songs.creatSong(name,SingerName,ReleasedDay,SongLength);
                   if(Alb.addSOngs(song))
                       System.out.println("Songs added ! ");
                   else
                       System.out.println("Error ! ");
                }
            }`enter code here`
        }
    //Album Class
    package DevAyo;

    import java.util.ArrayList;
    import java.util.Scanner;

    public class Album {
        public  String AlbumName;
        private static Scanner scanner = new Scanner(System.in);
        ArrayList<Songs> ListofSongs;

        public Album(String albumName) {
            AlbumName = albumName;
            this.ListofSongs=new ArrayList<Songs>();
        }

        public boolean addSOngs(Songs song){
            if(findSong(song.getSongName())>=0){
                System.out.println("Songs aLready Exisit ! ");
                return false;
            }
            ListofSongs.add(song);
            return true;
     }

     private int findSong(String songName){
            for(int i=0; i<ListofSongs.size();i++){
                Songs song = ListofSongs.get(i);
                if(song.getSongName().equals(songName)){
                    return i;
                }

            }
         return -1;
     }

     public void printSongs(){
            for(int i=0;i<this.ListofSongs.size();i++){
                System.out.println("Song Name: "+ this.ListofSongs.get(i).getSongName()+"\n"
                +"Singer: "+this.ListofSongs.get(i).getSongSinger()+"\n"+"Song Released Day: "+this.ListofSongs.get(i).getSongDay()+
                        "\n"+"Song Length: "+this.ListofSongs.get(i).songLength+" min");
            }
     }

    }

//Songs Class

package DevAyo;

public class Songs {
    public static String songName;
    public static String songSinger;
    public static String songDay;
    public static int songLength;

    public Songs(String songName, String songSinger, String songDay, int songLength) {
        this.songName = songName;
        this.songSinger = songSinger;
        this.songDay = songDay;
        this.songLength = songLength;
    }

    public String getSongName() {
        return this.songName;
    }

    public String getSongSinger() {
        return this.songSinger;
    }

    public String getSongDay() {
        return this.songDay;
    }

    public int getSongLength() {
        return this.songLength;
    }


    public static Songs creatSong(String name, String singerName, String releasedDay, int songLength){
        return new Songs(songName,songSinger,songDay,songLength);
    }
}

我只是尝试了很多,所以我无法理解为什么它会显示null,我已经在两个类中都创建了构造函数,但似乎仍然不起作用,说实话我已经在互联网上搜索了这个问题,但是说我应该添加构造函数我已经添加了它们,但无论如何都希望清楚代码,希望大家可以帮助谢谢。

Scanner.nextInt(); 留下一个空行,以便下次您调用Scanner.nextLine(); 它会给你那条空行。 如果要避免这种情况,可以执行以下两个选项之一:

  1. 解析

    始终使用Scanner.nextLine()并将其Scanner.nextLine()为所需的变量类型。 示例: int a = Integer.parseInt(scanner.nextLine());

  2. 读空行

    总是使用nextInt()立即调用nextLine()示例: int a = scanner.nextInt(); scanner.nextLine() nextLine() int a = scanner.nextInt(); scanner.nextLine() int a = scanner.nextInt(); scanner.nextLine()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM