带有用户输入的 while 循环 - 无法让 while 循环停止请求用户输入

时间:2021-03-07 19:30:42

标签: java

一旦用户输入“停止”,我试图让 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;
}

1 个答案:

答案 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;
}