Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Loops 我的while语句与我的BigDecimal不兼容_Loops_While Loop_Bigdecimal - Fatal编程技术网

Loops 我的while语句与我的BigDecimal不兼容

Loops 我的while语句与我的BigDecimal不兼容,loops,while-loop,bigdecimal,Loops,While Loop,Bigdecimal,这个代码不起作用,我想知道为什么。问题似乎在while语句中,我似乎找不到问题所在。请在这里帮忙 public static void main(String[] args) { //Creates the BigDecimal and scanner used BigDecimal answer = new BigDecimal("0"); Scanner scan = new Scanner(System.in); //While statemen

这个代码不起作用,我想知道为什么。问题似乎在while语句中,我似乎找不到问题所在。请在这里帮忙

    public static void main(String[] args) {

    //Creates the BigDecimal and scanner used
    BigDecimal answer = new BigDecimal("0");
    Scanner scan = new Scanner(System.in);

    //While statement to repeat if they dont answer 1 or 2
    while (! answer.equals("1") && ! answer.equals("2")) {
        //Asks user to input the number 1 or 2
        System.out.print("Enter the number 1 or 2: ");
        //Takes in users answer
        answer = scan.nextBigDecimal(); 
    }

    //Uses printf to print what they typed in
    System.out.printf("You entered the number %s.", answer);
    scan.close();

}
您正在将BigDecimal对象与字符串“1”和“2”进行比较

这个怎么样

while (! answer.equals("1") && ! answer.equals("2")) {

请解释“不工作”是什么意思。它编译吗?它是否编译但抛出错误?错误消息是什么?它运行但没有给出正确的答案吗?它给出了什么答案?
while (! answer.equals(new BigDecimal("1")) && ! answer.equals(new BigDecimal("2"))) {