我试图运行的循环将初始化,但不会在第一个循环后继续运行。由于我知道问题出在哪里,我拿出了大部分代码来修复这个循环。我做了第二个选择后,循环将不会运行。感谢您的任何帮助。while循环初始化但不循环java
public static void main(String[] args)
{
String number; // enter number
int stringLength = 0;
String selection = "y" ;
// Create a Scanner object to read input.
Scanner keyboard = new Scanner(System.in);
// PrintWriter outputFile = new PrintWriter("outDataFile.txt");
// outputFile.close();
while (selection == "y")
{
// Get the user's number.
System.out.print("Write your number ");
number = keyboard.nextLine();
System.out.print("y/Y to continue, any else to exit");
selection = keyboard.nextLine();
}
}
使用'=='来比较字符串并不符合您在Java中所期望的。参见[如何比较Java中的字符串?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)。 – Jesper