我不明白爲什麼while循環有效,done被聲明爲false,while循環的條件是在done不是false時運行。那麼爲什麼它會被執行? !完成和完成!=假?!false和!= false有什麼區別
import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListDemo
{
public static void main(String[] args)
{
ArrayList<String> toDoList = new ArrayList<String>(20);
System.out.println("Enter list entries, when prompted.");
boolean done = false;
String next = null;
String answer;
Scanner keyboard = new Scanner(System.in);
while(!done)
{
System.out.println("Input an entry:");
next = keyboard.nextLine();
toDoList.add(next);
System.out.println("More items for the list? ");
answer = keyboard.nextLine();
if(!(answer.equalsIgnoreCase("yes")))
{
done = true;
}
System.out.println("The list contains:");
for(String entry: toDoList)
{
System.out.println(entry);
}
}
}
}
!false == true。 –
'done == false' - '!done ==!false == true' - 因此循環運行。混淆在哪裏? – PPartisan
done是false,而while循環只有在done完成時纔會循環等於true,爲什麼它會循環? – peace9000