Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扫描仪和阵列列表_Java - Fatal编程技术网

Java 扫描仪和阵列列表

Java 扫描仪和阵列列表,java,Java,我被困在一个家庭作业上,这是我这学期的最后一个项目。我必须创建一个包含歌曲类和MP3播放器类的程序。我遇到的问题是从用户那里获取输入并将其放入arrayList,然后将其打印出来。有人能帮我向正确的方向走吗 package program3; import java.util.Scanner; /** * * @author Seth */ public class Main { /** * @param args the command line arguments

我被困在一个家庭作业上,这是我这学期的最后一个项目。我必须创建一个包含歌曲类和MP3播放器类的程序。我遇到的问题是从用户那里获取输入并将其放入arrayList,然后将其打印出来。有人能帮我向正确的方向走吗

package program3;

import java.util.Scanner;





/**
 *
 * @author Seth
 */
 public class Main {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here


    Scanner sc = new Scanner(System.in);

    System.out.print("Enter the song's title:");
        String title = sc.nextLine();
    System.out.print("Enter the song's artist:");
        String artist = sc.nextLine();
    System.out.print("Enter the song's duration:");
        float duration = sc.nextFloat(); 
    System.out.print("Enter the size of the songs file: "); 
        int fileSize = sc.nextInt(); 

    Song song = new Song(title, artist, duration, fileSize);
        song.add(song);

    for (int i = 0; i<song.length; i++){
        System.out.println("Song " + i + " title is: " + 
            song.getTitle() + ", the artist is " + 
            song.getArtist() + " and the duration is " + 
            String.valueOf(song.getDuration()) + ".");
    }

}

}






package program3;

import java.util.ArrayList;



 /**
 *
 * @author Seth
 */
public class MP3_Player {

private int freeSpace;
private int storageCapacity;

ArrayList<Song> songList = new ArrayList<Song>(); 



public MP3_Player( )
{ 
   storageCapacity = 10;
   freeSpace = storageCapacity; 

}

public MP3_Player(int storage)
{
    storageCapacity = storage; 
    freeSpace = storageCapacity;

}    

public String toString()
{
    return "MP3 player with capacity " + storageCapacity + 
            ", " + freeSpace + " left, contains:\n" + 
    songList.toString().replace("[","").replace("]","").replace(",","\n");
}

public int getFreeSpace()
{
    return freeSpace; 
}

public int getStorageCapacity()
{
    return storageCapacity; 
}

public ArrayList<Song> getSongList()
{
    return songList; 
}

public void setStorageCapacity(int newStorageCapacity)
{
    storageCapacity = newStorageCapacity; 
}

public void setFreeSpace(int newFreeSpace)
{
    if (newFreeSpace >= 0) {
        freeSpace = newFreeSpace; 
    }
}


public void addSong(Song s)
{
     if(s.getFileSize() <= freeSpace)
     {
         songList.add(s);
         System.out.println(s.getTitle() + " added to the MP3 player."); 
         freeSpace -= s.getFileSize(); 
     }
     else
     {
         System.out.println("Cannot add " + s.getTitle() + 
                 " not enough free space.");
     }

}


public void removeSong(Song s)
{
    boolean songRemoval = false; 
    for (Song i : songList)
    {
        if(s.equals(i))
        {
            freeSpace += s.getFileSize(); 
            songRemoval = true; 
            System.out.println(s.getTitle() + 
                    " is removed from the MP3 Player."); 
        }
        else if(s.equals(i))
        {
            System.out.println("This song isn't stored in the MP3 Player."); 
        }
    }
    if(songRemoval)
    {songList.remove(s); 
}
}
}
package程序3;
导入java.util.Scanner;
/**
*
*@作者赛斯
*/
公共班机{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
//此处的TODO代码应用程序逻辑
扫描仪sc=新的扫描仪(System.in);
System.out.print(“输入歌曲的标题:”);
字符串title=sc.nextLine();
System.out.print(“输入歌曲的艺术家:”);
字符串艺术家=sc.nextLine();
System.out.print(“输入歌曲的持续时间:”);
浮动持续时间=sc.nextFloat();
System.out.print(“输入歌曲文件的大小:”);
int fileSize=sc.nextInt();
歌曲=新歌(标题、艺术家、持续时间、文件大小);
歌曲。添加(歌曲);
对于(int i=0;i=0){
自由空间=新自由空间;
}
}
公共歌曲(歌曲s)
{

如果(s.getFileSize()那么这里有一个解决您问题的方法…(根据我的说法):-)

  • 首先询问用户,他想添加多少首歌曲
  • 现在循环N次并重复以下步骤
  • 让用户输入歌曲的详细信息说歌曲有4个东西要添加标题、艺术家、somemoredetails、大小
  • 让sound类添加所有细节,这样在这个循环之后,您将拥有N个歌曲对象
  • 那么,如何打印所有歌曲及其信息呢
  • 代码如下:

    //loop through the arraylist like this 
    for (Song s:arraylistofobjcts){
       s.o.p("Songs"+s.getTitle()+s.getArtist()+s.getWriter()+s.getFileSize())
    }
    
    //就这样

    **So this the song class**
    
  • **公开课歌曲{

    private String title,writer,artist;
        private int size;
    
    public Song(String title,String writer, String artist,int size){
    
        this.setTitle(title);
        this.setWriter(writer);
        this.setArtist(artist);
                this.setSize(size); 
    }
    public int getSize() {
        return this.size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public String getTitle() {
        return this.title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getArtist() {
        return this.artist;
    }
    public void setArtist(String artist) {
        this.artist = artist;
    }
    public String getWriter() {
        return this.writer;
    }
    public void setWriter(String writer) {
        this.writer = writer;
    }
    
    }**


    cheers

    用户需要输入什么?只是一首歌?还是一首歌的所有细节?用户需要输入所有内容。程序应该要求提供歌曲标题和文件大小。如果有可用空间,则程序应该将歌曲添加到ArrayList中。此外,如果用户输入的标题已经超过然后它应该从ArrayList中删除标题(我认为我使用的删除方法是正确的)。然后,您必须使用filesize参数更新构造函数。您是说公共MP3_播放器(){storageCapacity=10;freeSpace=storageCapacity;}我指的是歌曲的文件大小,因为构造函数只有3个参数(标题、艺术家和持续时间)。