非常基本的Java:For Loop in Java方法赢得';跑不动
我以前从未就此提出过任何问题,但我非常感谢任何人能提供的任何帮助。我目前正在学习Java的基础知识,因此这很可能是一个非常基本的问题。当我调用这个方法时,似乎什么都没有发生,我也不知道为什么。我可以将其更改为键入void并使用system.print,但我不希望这样,无论如何,代码如下:非常基本的Java:For Loop in Java方法赢得';跑不动,java,netbeans,for-loop,methods,Java,Netbeans,For Loop,Methods,我以前从未就此提出过任何问题,但我非常感谢任何人能提供的任何帮助。我目前正在学习Java的基础知识,因此这很可能是一个非常基本的问题。当我调用这个方法时,似乎什么都没有发生,我也不知道为什么。我可以将其更改为键入void并使用system.print,但我不希望这样,无论如何,代码如下: public double calcTotal() { double total = 0.00; for (int i = 0; i < jSongs.size(); i++)
public double calcTotal()
{
double total = 0.00;
for (int i = 0; i < jSongs.size(); i++)
{
total += jSongs.get(i).getPrice();
}
return total;
}
这是jukebox类,我确信它充满了错误:
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;
}
}
你的描述让我觉得你在这样调用你的方法
calcTotal();
而不是实际使用方法返回的值
double total = calcTotal();
System.out.println(total);
你的代码看起来不错。addSong的功能可能更简单。但是问题是,您没有打印函数的结果calcTotal()jSongs.size()返回什么值?如果我没弄错的话,你怎么称呼
calcTotal()
?2。jb.calcTotal();这个方法是正确的。PostjSongs
。或Post您对calcTotal()的结果所做的一切我所做的只是显示calcTotal(),没有对它做任何其他事情,问题是它不会显示,除非我将方法更改为void并使用system.print。jSongs只是一个标准的arrayList,其中包含基本对象。当我像这样调用它时,它就工作了,但我认为我应该能够调用该方法,因为该方法应该返回total?还是我完全错了这个假设?不管怎样,谢谢你,好先生。@Michael\D该方法返回一个值。这就是它的全部功能。你需要特别做一些有价值的事情。它不会自动打印的。我明白了,谢谢。我才刚开始学习Java,请原谅我的愚蠢。@Michael\D不客气。别担心,我们都经历过。除非有遗漏,否则要考虑接受答案。
calcTotal();
double total = calcTotal();
System.out.println(total);