gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 13:41:48 24 4
gpt4 key购买 nike

我以前从未问过这方面的问题,但我很感激任何人可以提供的帮助。我目前正在学习 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com