我被要求获取用户输入并忽略不在 -30 和 40 范围内的值。为了跳过无效数字,我使用“继续”语句。我在谷歌上搜索过消息来源说继续/中断是不好的做法。 IDE 还会发出“不必要的继续”警告。下面的代码是解决此问题的良好做法吗?我应该忽略警告还是解决它?
我的代码如下所示:
public class Temperatures
{
@SuppressWarnings("UnnecessaryContinue")
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
// Write your code here.
while(true)
{
//ask user for input
double userInput = Double.parseDouble(reader.nextLine());
//makes sure temperature is within range, if it isn't ignores value and moves on
if (userInput < -30.0 || userInput > 40.0)
{
continue;
}
//adds value to graph
else
{
Graph.addNumber(userInput);
}
}
}
最佳答案
IDE 可能会显示警告,因为如果您删除了 continue,您的代码将完全相同。想一想。
关于java - 除了使用 'continue' 之外,还有另一种使用 while(true) 循环忽略值的方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29271271/