Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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 尽管计算错误,但扫描程序表达式仍在继续的While循环_Java_Loops_While Loop - Fatal编程技术网

Java 尽管计算错误,但扫描程序表达式仍在继续的While循环

Java 尽管计算错误,但扫描程序表达式仍在继续的While循环,java,loops,while-loop,Java,Loops,While Loop,对java来说非常陌生,并且想知道为什么即使来自扫描器的输入是“N”,这个while语句也不会结束,从而将表达式求值为false import java.util.*; public class addSongs { public static void main (String[]args){ Scanner songAdd = new Scanner(System.in); Scanner addContinue = new Scanner(Sy

对java来说非常陌生,并且想知道为什么即使来自扫描器的输入是“N”,这个while语句也不会结束,从而将表达式求值为false

import java.util.*;

public class addSongs {

    public static void main (String[]args){

        Scanner songAdd = new Scanner(System.in);
        Scanner addContinue = new Scanner(System.in);
        String ceaseAdd = new String();
        ceaseAdd = "Y";

        while(ceaseAdd != "N")
            System.out.println("Enter track title");
            String newSong = songAdd.nextLine();
            MP3_catalogue.title.add(newSong);
            System.out.println("Enter artist name");
            String newArtist = songAdd.nextLine();
            MP3_catalogue.artist.add(newArtist);
            System.out.println("Enter duration");
            String newDuration = songAdd.nextLine();
            MP3_catalogue.duration.add(newDuration);
            System.out.println("Would you like to add another song? Y/N");
            ceaseAdd = addContinue.nextLine().toUpperCase();
    }

}

在java中,不能将字符串与“=”进行比较。 “==”检查它是否是完全相同的对象,而不是它是否具有相同的值。 要检查其文本是否相等,必须使用

string.equals()
在您的示例中,它将如下所示:

public static void main (String[]args){

Scanner songAdd = new Scanner(System.in);
Scanner addContinue = new Scanner(System.in);
String ceaseAdd = new String();
ceaseAdd = "Y";

while(!ceaseAdd.equals("N"))
    System.out.println("Enter track title");
    String newSong = songAdd.nextLine();
    MP3_catalogue.title.add(newSong);
    System.out.println("Enter artist name");
    String newArtist = songAdd.nextLine();
    MP3_catalogue.artist.add(newArtist);
    System.out.println("Enter duration");
    String newDuration = songAdd.nextLine();
    MP3_catalogue.duration.add(newDuration);
    System.out.println("Would you like to add another song? Y/N");
    ceaseAdd = addContinue.nextLine().toUpperCase();
}
while(ceaseAdd != "N")
    System.out.println("Enter track title");
String newSong = songAdd.nextLine();
MP3_catalogue.title.add(newSong);
System.out.println("Enter artist name");

除了已经指出的字符串检查。缺少
{}
。正确缩进的代码如下所示:

public static void main (String[]args){

Scanner songAdd = new Scanner(System.in);
Scanner addContinue = new Scanner(System.in);
String ceaseAdd = new String();
ceaseAdd = "Y";

while(!ceaseAdd.equals("N"))
    System.out.println("Enter track title");
    String newSong = songAdd.nextLine();
    MP3_catalogue.title.add(newSong);
    System.out.println("Enter artist name");
    String newArtist = songAdd.nextLine();
    MP3_catalogue.artist.add(newArtist);
    System.out.println("Enter duration");
    String newDuration = songAdd.nextLine();
    MP3_catalogue.duration.add(newDuration);
    System.out.println("Would you like to add another song? Y/N");
    ceaseAdd = addContinue.nextLine().toUpperCase();
}
while(ceaseAdd != "N")
    System.out.println("Enter track title");
String newSong = songAdd.nextLine();
MP3_catalogue.title.add(newSong);
System.out.println("Enter artist name");
如果没有
{}
,则
while
仅适用于第一行。应该是这样的:

while(!ceaseAdd.equals("N")){      
    System.out.println("Enter track title");
    String newSong = songAdd.nextLine();
    MP3_catalogue.title.add(newSong);
    System.out.println("Enter artist name");
    String newArtist = songAdd.nextLine();
    MP3_catalogue.artist.add(newArtist);
    System.out.println("Enter duration");
    String newDuration = songAdd.nextLine();
    MP3_catalogue.duration.add(newDuration);
    System.out.println("Would you like to add another song? Y/N");
    ceaseAdd = addContinue.nextLine().toUpperCase();
}

String
值与
String
equals
方法进行比较,而不是与
=
进行比较=。这:应该有助于您理解@rgetman的评论为什么是正确的。您的代码和您对发生的事情的描述不一致。您说即使输入了“N”,它也会运行。使用此代码,您将永远无法为
ceaseAdd
输入任何内容,它将永远打印“输入曲目标题”。