是否可以以这种方式使用布尔表达式?

时间:2016-10-22 08:07:20

标签: java netbeans-8

我目前正在学习java脚本并尝试新事物,例如我希望看到如果它通过结束数字检测到起始数字,我是否可以设置一个布尔表达式。

或者换句话说,我想要的是3到8。 我将明确表示我正在使用netbeans IDE。

在我最后的if if语句中,我希望它结束​​询问过程。我希望这是一个简单的解决方案,但除非我在if语句中创建更多其他内容,否则我无法想到任何可以实现此目的的东西。

扫描仪输入=新扫描仪(System.in);

int[][] table;

boolean stopasking = true;



    while (stopasking = true){

    System.out.println("Let's make a magic Square! How big should it be? ");
    int size = input.nextInt();


    if (size < 0)
    {
        System.out.println("That would violate the laws of mathematics!");
        System.out.println("");
    }
    else if (size >= 9)
    {
        System.out.println("That's huge! Please enter a number less than 9.");
        System.out.println("");
    }
    else if (size <= 2)
    {
        System.out.println("That would violate the laws of mathematics!");
        System.out.println("");
    }
    else if (size == 3)
    {
        stopasking = false;
    }


    }

1 个答案:

答案 0 :(得分:1)

您使用了assignmnent运算符WakefulBroadcastReceiver.completeWakefulIntent() 您应该使用=代替

==时条件size<=2也成立,因此您可以使用一个size<0

if

您可以以这种方式使用布尔表达式作为退出循环的条件。有人会说它比while(stopasking){ if (size <= 2) { System.out.println("That would violate the laws of mathematics!\n"); } else if (size >= 9){ System.out.println("That's huge! Please enter a number less than 9.\n"); } else if (size == 3){ stopasking = false; } } 更优雅。