一旦用户输入“停止”,我试图让 while 循环停止要求用户输入,但我无法让它停止要求用户输入。
public static void main(String[] args)
{
exampletoday llist=new exampletoday();
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter the first operator or operand: ");
String first=keyboard.nextLine();
llist.addNode(first);
while(true)
{
System.out.println("Enter the next operand or operator or stop to stop: ");
String next=keyboard.nextLine();
llist.addNode(next);
if(next=="stop")
break;
}
答案 0 :(得分:-1)
使用 .equals()
比较字符串。
public static void main(String[] args)
{
exampletoday llist=new exampletoday();
Scanner keyboard=new Scanner(System.in);
System.out.println("Enter the first operator or operand: ");
String first=keyboard.nextLine();
llist.addNode(first);
while(true)
{
System.out.println("Enter the next operand or operator or stop to stop: ");
String next=keyboard.nextLine();
llist.addNode(next);
if(next.equals("stop"))
break;
}