非常基本的Java:Java方法中的For-Loop将无法运行

时间:2013-12-19 15:35:58

标签: java netbeans for-loop methods

我以前从未问过这个问题,但我很感激任何人都可以提供帮助。我目前正在学习Java的基础知识,所以这很可能是一个非常基本的问题。当我称这种方法时,似乎没有任何事情发生,我无法弄清楚为什么。我可以将其更改为输入void并使用system.print,但我不愿意,无论如何这里是代码:

public double calcTotal()
{
    double total = 0.00;

    for (int i = 0; i < jSongs.size(); i++)
    {
        total += jSongs.get(i).getPrice();
    }

    return total;
}

我认为如果我只是向你们展示了很多内容会更容易,这就是调用方法来测试它们的应用程序:

public class JukeboxApp {
    public static void main(String[] args) {
        Song s1 = new Song("Metallica", "The Unforgiven", 1.25, 6.23);
        Song s2 = new Song("Test Artist 2", "Test Title 2", 4.00, 3.40);
        Song s3 = new Song("Test Artist 3", "Test Title 3", 6.00, 2.50);
        Jukebox jb = new Jukebox();
        jb.addSong(s1);
        jb.addSong(s2);
        jb.addSong(s3);
        jb.displaySongs();
        jb.removeSong("The Unforgiven");
        jb.searchSong("Test Title 2");
        jb.calcTotal();
    }

}

这是自动点唱机类,我确信它充满了错误:

import java.util.ArrayList;
public class Jukebox {
private String name;
private ArrayList<Song> jSongs;

public Jukebox()
{
    name = "Primary Jukebox";
    jSongs = new ArrayList<Song>();
}

public String getName()
{
    return name;
}

public double calcTotal()
{
    double total = 0.00;

    for (int i = 0; i < jSongs.size(); i++)
    {
        total += jSongs.get(i).getPrice();
    }

    return total;
}

public void searchSong(String sTitle)
{
    boolean check = false;
    if ( jSongs.size() == 0 ) {
        System.out.println("The are no songs in the list.");
        check = true;
    } else if ( jSongs.size() != 0 ) {
        for ( int i = 0; i < jSongs.size(); i++ ) {
            if ( jSongs.get(i).getTitle().equals(sTitle) == true ) {
                check = true;
                System.out.println(jSongs.get(i));
            }
        }
    }
    if ( check == false ) {
        System.out.println("The searched song could not be found.");
    }
}

public String searchArtist(String sArtist)
{
    int countMatch = 0;
    for (int i = 0; i < jSongs.size(); i++) {
        if ( jSongs.get(i).getArtist().equals(sArtist) ) {
            countMatch++;
            return jSongs.get(i).getTitle();
        } else if ( countMatch == 0 ) {
            return "The requested artist could not be found.";
        }
    }
    return "If you would like to search for another artist, please enter the corresponding number.";
}

public void addSong(Song s1)
{
    boolean check = false;

    if ( jSongs.size() == 0 ) {
        System.out.println("Your song will be added to the list.");
        jSongs.add(s1);
        return;
    } else if ( jSongs.size() != 0 ) {
        for ( int i = 0; i < jSongs.size(); i++ ) {
            if ( jSongs.get(i) == s1 ) {
                check = true;
            }
        }
    }
    if ( check == false ) {
        System.out.println("Your song will be added to the list.");
        jSongs.add(s1);
    } else if ( check == true ) {
        System.out.println("Your song is already in the list.");
    }
}

public void removeSong(String title)
{
    boolean check = false;
    for ( int i = 0; i < jSongs.size(); i++ ) {
        if ( jSongs.get(i).getTitle().equals(title) ) {
            jSongs.remove(i);
            check = true;
        }
    }
    System.out.println(check);
}

public void displaySongs()
{
    for ( int i = 0; i < jSongs.size(); i++ ) {
        System.out.println(jSongs.get(i));
    }
}

public Song showMostExpensive()
{
    double price = 0.00;
    Song mostESong = new Song();
    for ( int i = 0; i < jSongs.size(); i++ ) {
        if ( jSongs.get(i).getPrice() > price ) {
            price = jSongs.get(i).getPrice();
            mostESong = jSongs.get(i);
        }
    }
    return mostESong;
}

public Song showShortest()
{
    double length = 500.00;
    Song shortest = new Song();
    for ( int i = 0; i < jSongs.size(); i++ ) {
        if ( jSongs.get(i).getLength() < length ) {
            length = jSongs.get(i).getLength();
            shortest = jSongs.get(i);
        }
    }
    return shortest;
}

public Song mostPlayed()
{
    int count = 0;
    Song mostPSong = new Song();
    for ( int i = 0; i < jSongs.size(); i++ ) {
        if ( jSongs.get(i).getCount() > count ) {
            count = jSongs.get(i).getCount();
            mostPSong = jSongs.get(i);
        }
    }
    return mostPSong;
}
}

这是创建歌曲对象的类:

public class Song {
private String artist;
private String title;
private double price;
private int playCount;
private double length;

public Song()
{
    artist = "unknown";
    title = "unknown";
    price = 0.00;
    length = 0.00;
    playCount = 0;
}

public Song(String artist, String title, double price, double length)
{
    this.artist = artist;
    this.title = title;
    this.price = price;
    this.length = length;
    playCount = 0;
}

public String getArtist()
{
    return artist;
}

public String getTitle()
{
    return title;
}

public double getPrice()
{
    return price;
}

public int getCount()
{
    return playCount;
}

public double getLength()
{
    return length;
}

public void changePrice(double newPrice)
{
    price = newPrice;
}

public void playSong()
{
    playCount++;
    System.out.println(title + " is now playing." + "\n" + toString());
}

public String toString()
{
    return artist + "\n"
    + title + "\n"
    + price + "\n"
    + length;
}
}

2 个答案:

答案 0 :(得分:1)

您的描述让我觉得您正在调用您的方法

calcTotal();

而不是实际使用方法返回的值

double total = calcTotal();
System.out.println(total);

答案 1 :(得分:0)

您的代码似乎很好。可能addSong的功能可能更容易。但问题是你没有打印函数 calcTotal()的结果。