Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 错误:在程序中找不到符号_Java_Arrays_Types - Fatal编程技术网

Java 错误:在程序中找不到符号

Java 错误:在程序中找不到符号,java,arrays,types,Java,Arrays,Types,我正在尝试创建一个程序,在播放列表中添加和删除歌曲,但我不断收到此错误,即如果(!found){此分区代码只是通过在数组中移动以下歌曲来删除歌曲,我只需要一些有关此错误的帮助,我的程序将完成。谢谢 import javax.swing.JOptionPane; public class MusicPlaylist { // the maximum number of songs the music playlist can hold public static final

我正在尝试创建一个程序,在播放列表中添加和删除歌曲,但我不断收到此错误,即如果(!found){此分区代码只是通过在数组中移动以下歌曲来删除歌曲,我只需要一些有关此错误的帮助,我的程序将完成。谢谢

 import javax.swing.JOptionPane;

public class MusicPlaylist {

    // the maximum number of songs the music playlist can hold

    public static final int MAX_SONGS = 3;

    public static void main(String[] args) {
        // create arrays and size counter
        int size = 0;
        String[] songsTitles = new String[MAX_SONGS];
        double[] songsLength = new double[MAX_SONGS];

        // Enter Songs
        while (true) {
            // check max number of songs
            if (size >= MAX_SONGS) {
                JOptionPane.showMessageDialog(null, "Max number of songs reached.");
                break;
            }
            // Get title or exit command
            String title = JOptionPane
                    .showInputDialog("Insert song #" + (size + 1) + " title (or Cancel to finish adding songs):");
            if (title == null)
                break;

            // check if there is a title
            if (title.isEmpty()) {
                JOptionPane.showMessageDialog(null, "Title must not be null.", "Error", JOptionPane.ERROR_MESSAGE);
                continue; // start from the top

                break;
            }
            // Get length
            double length;
            try {
                length = Double
                        .parseDouble(JOptionPane.showInputDialog("Insert song" + (size + 1) + " length is minutes: "));
                if (length <= 0)
                    throw new NumberFormatException();
            } catch (NumberFormatException e) {
                JOptionPane.showMessageDialog(null, "Length must not be a valid, posite double number.", "Error",
                        JOptionPane.ERROR_MESSAGE);
                continue; // start from the top
            }
            // Add song
            songsTitles[size] = title;
            songsLength[size++] = length;

        }

        // show list and allow to remove songs
        while (size > 0) { // if size <=0, nothing to remove
            // show list
            String list = "";
            for (int i = 0; i < size; i++) {
                list += songsTitles[i] + "  '+ songsLength[i] + 'min/n";
            }
            JOptionPane.showMessageDialog(null, list);

            // get title or exit command
            String title = JOptionPane.showInputDialog("Insert title to delete(or Cancel to finish removing songs):");
            if (title == null)
                break;

            // Find and delete song or show error message
            boolean found = false;
            for (int i = 0; i < size; i++) {
                if (songsTitles[i].equals(title))
                    // Delete song by shifting following songs over it in the
                    // array
                    for (int j = i + 1; j < size; j++) {
                        songsTitles[j - 1] = songsTitles[j];
                        songsLength[j - 1] = songsLength[j];
                    }
                size--;
                found = true;
            }
        }
         **if (!found) {**
            JOptionPane.showMessageDialog(null, "Song not found", "Error", JOptionPane.ERROR_MESSAGE);
        }
        // Show report
        String list = "*** REPORT ***/n";
        double total = 0;
        for (int i = 0; i < size; i++) {
            list += songsTitles[i] + "   " + songsLength[i] + "mins/n";
            total += songsLength[i];
        }
        list += "/nTotal length: " + total + " minutes.";
        JOptionPane.showMessageDialog(null, list);

    }

}
import javax.swing.JOptionPane;
公共类音乐播放列表{
//音乐播放列表可容纳的最大歌曲数
公共静态最终int MAX_SONGS=3;
公共静态void main(字符串[]args){
//创建数组和大小计数器
int size=0;
String[]songsTitles=新字符串[MAX_SONGS];
double[]歌曲长度=新的double[MAX_歌曲];
//输入歌曲
while(true){
//检查歌曲的最大数量
如果(大小>=最大歌曲数){
showMessageDialog(null,“达到的最大歌曲数”);
打破
}
//获取标题或退出命令
String title=JOptionPane
.showInputDialog(“插入歌曲”#“+(大小+1)+”标题(或取消以完成添加歌曲):”;
if(title==null)
打破
//检查是否有标题
if(title.isEmpty()){
showMessageDialog(null,“标题不能为null。”,“Error”,JOptionPane.Error\u消息);
continue;//从顶部开始
打破
}
//获取长度
双倍长度;
试一试{
长度=双倍
.parseDouble(JOptionPane.showInputDialog(“插入歌曲”+(大小+1)+“长度为分钟:”);

if(length 0){//if size您正在while循环内声明变量
found
,并尝试在while循环外访问它,因此存在错误,因为无法在外部访问局部变量

解决方案-像这样在while循环之前标记找到的变量

         boolean found = false;
         // show list and allow to remove songs
         while (size > 0) { // if size <=0, nothing to remove
         // show list
         ......

这里您使用的是
continue
break
语句将无法访问。因此您需要删除它。然后就可以开始了。

Javascript!=java
--它给了我一个音乐列表。java:69:title已经在main(java.lang.String[]中定义了对于这段代码-//获取标题或退出命令字符串title=JOptionPane.showInputDialog(“插入要删除的标题(或取消以完成删除歌曲):”);如果(title==null);将变量title的名称更改为其他名称。有两个变量的名称为title name。更改该名称,您就可以了。最后一个错误MusicPlaylist.java:55:变量长度可能尚未为这段代码初始化songsLength[size++]=length;将`double length;`更改为`double length=0.0`
// check if there is a title
 if (title.isEmpty()) {
  JOptionPane.showMessageDialog(null, "Title must not be null.","Error",       JOptionPane.ERROR_MESSAGE);
continue; // start from the top

break;
}