【问题标题】:Using arrays in Java, printing arrays在 Java 中使用数组,打印数组
【发布时间】:2015-11-08 21:19:13
【问题描述】:

http://puu.sh/l3owc/e16f0fe76f.png(讲师提示)

基本上,我试图让用户通过为歌曲标题输入输入 -1 来退出循环。出于某种原因,它对我不起作用,它只是将值 -1 作为歌曲名称。我遇到的另一个问题是,在输入每首歌曲后,我正在尝试按提示打印“所有剩余歌曲”。对我来说,当我希望它显示所有(我的提示是什么意思)输入的歌曲时,它只会删除前一个并显示最近的歌曲标题和长度。

然后,用户可以删除歌曲,之后应该会显示一个报告。我应该使用某种会不断添加的报告字符串吗?不知道该怎么做...我快要弄清楚了,只需要一些帮助。非常感谢这个网站的好心人

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) {

        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")) { 
                exitVar = true;
         }
        } while (songTitles[numSongs].equals("")); 



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

        } while (songLengths[numSongs] <= 0); 



        boolean addMore = true;

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

【问题讨论】:

    标签: java arrays


    【解决方案1】:

    您只设置了exitVar = true,但它仍会执行您在下面编写的所有操作。如果您希望它立即停止,您要么必须一直检查existVar 是否已经是true,或者您实际上可以使用break 和标签中止循环的进一步处理:

        songLoop: while (numSongs <= songTitles.length) {
            do {
                ...
                } else if (songTitles[numSongs].equals("-1")) {
                    break songLoop;
                }
          ...
    

    这样,在程序点击break songLoop 命令后,songLoop 中的任何内容都不会被执行。

    如果您不希望 songTitles[numSongs].equals("-1") 在该行之后不再出现这种情况,您将不得不覆盖该值或首先不将其写入那里(而是在一些临时变量中,并从进入数组)

    【讨论】:

    • 我明白了。我不允许在这个作业上使用休息。我可以用什么代替?另外,我只是想确保我阅读正确:每次用户完成输入歌曲时,我都会显示输入的所有歌曲,然后显示歌曲列表供用户输入歌曲名称以将其从列表,然后是另一个显示最终播放列表的打印件,对吧?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多