打印阵列,设置阵列

时间:2015-11-08 19:48:22

标签: java arrays

我的代码:

import javax.swing.JOptionPane;

public class IT106_Playlist {

  public static void main(String[] args){

  final int MAX_SONGS = 106;
  int totalDuration = 0;
  int numSongs=0;
  boolean exitVar=false;
  int i=0;

  String[] songTitles = new String[MAX_SONGS];
  int[] songLengths = new int[MAX_SONGS];

  while(exitVar==false && numSongs<=songTitles.length) {

     if (songTitles.equals("-1")){
        exitVar=true;
     }

  do{

     songTitles[numSongs] = JOptionPane.showInputDialog(null, "Enter a song name, or type -1 to exit");
     if (!songTitles.equals("")){
        JOptionPane.showMessageDialog(null, "Error: Please enter a valid song name, or type -1 to exit");
     }else{
     ++numSongs;
   }
  }while(!songTitles[numSongs].equals(""));

  do{
    try{ 
     songLengths[numSongs] = Integer.parseInt(JOptionPane.showInputDialog(null, "Enter a song length, e.g. 4, or type -1 to exit"));
     }catch (NumberFormatException e){
           songLengths[numSongs]=-1;
           JOptionPane.showMessageDialog(null, "Error: please enter a valid song length, e.g. 4, or type -1 to exit");
     }       
           totalDuration+=songLengths[numSongs];

  /* line 41 */ }while(songLengths[numSongs]=-1);

  boolean addMore=true;

  while((numSongs<=MAX_SONGS) && (addMore=true)){
     JOptionPane.showMessageDialog(null, "Song #" + i + ": "+ songTitles[i] + " length: " + songLengths[i] + "\n");
     i++;
    if(songTitles[i] ==null){
    addMore=false;
   }
  }
 }
}
}

您好。我试图找出为什么我的代码第41行有一个错误,说int无法转换为布尔值,我想知道为什么/如何解决这个问题。

基本上,我正在制作一个播放列表。用户分别输入歌曲名称和长度(两个不同的阵列),然后一旦他们达到最大歌曲数量,或者他们希望退出,他们可以通过键入-1来实现。

然后,用户可以从他们的播放列表中删除歌曲,然后他们可以退出,打印所有歌曲和总持续时间,以及歌曲的总数。

我们不能使用arrayLists或任何方法。

我的问题是,1。我的设置好吗,以及2.如何在输入每个歌曲名称后打印歌曲?我非常接近想出这个......谢谢你!

3 个答案:

答案 0 :(得分:2)

注意,这里有两个问题。其中一个是显示涉及歌曲长度的错误,这应该是:

while (songLengths[numSongs] == -1);

你还有一个隐藏的错误线,你运行while ((numSongs <= MAX_SONGS) && (addMore = true))。这是 addMore分配给true,您希望运行(addMore == true)进行比较。

编辑刚发现错误涉及第30行。您正在检查String[]是否等于"",如此:if (!songTitles.equals("")) {。这是不正确的,你也是!,你要检查if (songTitles[numSongs].equals("")) {

编辑2原来这不是第30行的整个问题。在do while,在该行,您有A)!您不想要的地方它,以及B)如果用户确实输入了正确的歌曲名称,之前在do while,你增加numSongs,现在你需要得到前一个比较,如下所示:

} while (songTitles[numSongs-1].equals(""));

编辑3&amp; 4我现在已经完全阅读了代码,这就是我认为您正在寻找的内容。我发表了评论,解释了一些主要变化。此外,现在处理no System.exit(0);用法。

import javax.swing.JOptionPane;

public class IT106_Playlist {

    public static void main(String[] args) {

        final int MAX_SONGS = 106;
        int totalDuration = 0;
        int numSongs = 0;
        boolean exitVar = false;
        int i = 0;

        String[] songTitles = new String[MAX_SONGS];
        int[] songLengths = new int[MAX_SONGS];

        while (numSongs <= songTitles.length) {
            // Removed your check to see if the song name is -1, and placed it later on
            do {

                songTitles[numSongs] = JOptionPane.showInputDialog(null,
                        "Enter a song name, or type -1 to exit");
                if (songTitles[numSongs].equals("")) {
                    JOptionPane.showMessageDialog(null,"Error: Please enter a valid song name, or type -1 to exit");
                } else if (songTitles[numSongs].equals("-1")) { // Checks if -1
                    exitVar = true;
                    System.exit(0); // This is how to quickly exit your program safely
                }
            } while (songTitles[numSongs].equals("")); // Keep asking the user for input if they don't provide any

            if (exitVar) {
                break;
            }

            do {
                try {
                    songLengths[numSongs] = Integer
                            .parseInt(JOptionPane
                                    .showInputDialog(null,
                                            "Enter a song length, e.g. 4."));
                    if (songLengths[numSongs] > 0) { // Check if the length is more than 0 (assuming no songs are 0 in length)
                        totalDuration += songLengths[numSongs]; // Add to totalDuration only if it is a valid length
                    } else { // If song length is invalid, display error
                        songLengths[numSongs] = -1;
                        JOptionPane
                        .showMessageDialog(null,
                                "Error: please enter a valid song length, e.g. 4.");
                    }
                } catch (NumberFormatException e) { // Catch if could not parse int
                    songLengths[numSongs] = -1;
                    JOptionPane
                            .showMessageDialog(null,
                                    "Error: please enter a valid song length, e.g. 4.");
                }

            } while (songLengths[numSongs] <= 0); // Only do this if you don't have a valid song length

            boolean addMore = true;

            while ((numSongs <= MAX_SONGS) && (addMore == true)) { // addMore bug fixed
                // This is my addition, but to be more user friendly, I put in (i+1), so that the user doesn't question what "Song #0" is
                JOptionPane.showMessageDialog(null, "Song #" + (i+1) + ": "
                        + songTitles[i] + " length: " + songLengths[i] + "\n");
                i++;
                if (songTitles[i] == null) {
                    addMore = false;
                }
            }
            numSongs++; // Moved this way down here to avoid messing code up above
        }
    }
}

答案 1 :(得分:0)

   }while(songLengths[numSongs]=-1);

也许应该是

   }while(songLengths[numSongs]==-1);

前者始终将值-1分配给songLengths[numSongs],这会产生一个整数值,而在Java中,这不会转换为布尔值。

你也可能意味着while((numSongs<=MAX_SONGS) && addmore)

答案 2 :(得分:0)

看起来这条线是罪魁祸首:

while(songLengths[numSongs]=-1);

你似乎想要的应该是:

while(songLengths[numSongs] == -1);

单个=表示分配值;双==用于比较值。