java - 非常基本的 Java : For-Loop in Java method won't run

标签 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;
}
}

最佳答案

你的描述让我认为你是这样调用你的方法的

calcTotal();

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

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

关于java - 非常基本的 Java : For-Loop in Java method won't run,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20685670/

相关文章:

java - 将 Morphia 与 Java EE 结合使用

Java - 在没有质量损失的情况下压缩图像

JavaFx 8 - 一个具有无法着色的边框并且能够在中间包含文本的类

powershell - Get-ChildItem零结果输出

java - 我的代码给出了 java.lang.StringIndexOutOfBoundsException 并且命令以非零状态退出

bash - 我在 {1..$VAR} 中;做 echo $i;完毕

java - 如何在集合中搜索(使用比较器)

java - 将 SoundPool 与 URI 一起使用?

php - 网 bean "no tests executed"

java - 如何删除 NetBeans 中自动生成的注释? (//NOI18N)