Java – “while” with using not equal object >> not working

huangapple go评论83阅读模式
英文:

Java - "while" with using not equal object >> not working

问题

代码中为什么使用 "!"(非)运算符时不起作用,会出现 "while" 语句为空的情况?而当我移除 "!" 运算符时,它就正常工作...

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = "";
        while (!input.equals("quit")) {
            System.out.print("Input : ");
            input = scanner.next().toLowerCase();
            System.out.println(input);
        }
    }
}
英文:

Why is the below code not working when using "!" not equals; it gave me "while" statement has an empty body? and when I'm removing the "!" it works...

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String input = "";
        while (!input.equals("quit")); {
            System.out.print("Input : ");
            input = scanner.next().toLowerCase();
            System.out.println(input);
        }
    }
}

Java – “while” with using not equal object >> not working

答案1

得分: 3

你需要移除分号:

while(!input.equals("quit")) {
...
}
英文:

you have to remove the semicolon

while(!input.equals("quit")) {
...
}

答案2

得分: 1

移除循环中{前面的

英文:

Remove the ; in the loop before {

huangapple
  • 本文由 发表于 2020年8月20日 13:21:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/63498753.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定