-4

我的代码生成了一个异常。

X = x * 10;
Y = y * 10;

if ((pow(X, 2))+(pow(Y, 2)) <= 27225 and ((pow(X, 2))+(pow(Y, 2)) >= 1225))

用户将输入值xy如果值不低,程序将继续。我已将 x 和 y 声明为上面的双精度数。这部分上面有代码,不是开始。

我将此代码作为我的其他if功能

if (((pow(X, 2))+(pow(Y, 2)) > 27225) or ((pow(X, 2))+(pow(Y, 2)) <1225))
{
      cout<<"\n\nThe values you have chosen for the centre points are to not compatible with our program. Please choose smaller values.";//new
      cout<<"\n\nIf you do not understand, please ask the programmer for further explanation.";
}

但是,我根本无法让代码工作,因为没有施加限制,即使值太大/太小,它也会继续正常运行,谁能告诉我我做错了什么?谢谢

4

2 回答 2

5

我认为您的第一个if条件应该是检查它是否大于 1225 且小于 27225:

if ((pow(X, 2))+(pow(Y, 2)) <= 27225 && ((pow(X, 2))+(pow(Y, 2)) >= 1225))
//                              Here ^^

X正如您所拥有的,对于and的每个可能值都将满足条件Y;每个数字要么小于 27225,要么大于 1225。

对于第二个条件,只需执行以下操作else

if ((pow(X, 2))+(pow(Y, 2)) <= 27225 && ((pow(X, 2))+(pow(Y, 2)) >= 1225)) {
  // Distance from origin is within range
} else {
  // Distance from origin is outside range
}

请注意orand不常用,因为它们是||和的替代标记&&。我建议坚持||&&与大多数其他开发人员保持一致。

于 2013-03-22T17:18:05.743 回答
0

From your coding, it doesn't look like you have defined the data type of X, Y. Instead of

X = x * 10;
Y = y * 10;

Instead, try

 int X = x * 10;
 intY = y * 10;


if (((pow(X, 2))+(pow(Y, 2)) > 27225)) {
  cout<<"\n\nThe values you have chosen for the centre points are to not  compatible   with our program. Please choose smaller values.";//new
}
else if( ((pow(X, 2))+(pow(Y, 2)) <1225))
{
        cout<<"\n\nIf you do not understand, please ask the programmer for further  explanation.";
}

This would give you the two different results as expected

于 2013-03-22T17:25:03.493 回答