Shi*_*arn 1 c boolean-logic if-statement
我正在编写一个程序,通过插入3个整数来识别用户可以制作的三角形.之后,根据它所经历的条件和陈述,它确定它们可以制作的三角形是平衡(所有边都相同),还是SCALENE(没有边相同),或者ISOSCELES(双面相同).用户在每次询问时输入1-15cm之间的值.如果用户键入低于或高于限制的任何内容,程序会告知他们无法完成并且只是停止.
这是我的代码
    /*** Purpose: To create a C program which takes 3 integers and produces a result that shows which triangle(If valid) they have chosen.***/
   #include <stdio.h>
int main()
{
    /*** Declaring triangle variable sides ****/
  float sideA;
  float sideB;
  float sideC;
  char ch;
  printf("Lets explore triangles! Please insert a value for side 'A' of your triangle.\n");
  printf(" Ranging from 1-15cm.\n");
  while(scanf("%d", &sideA) != 1)
  {
    printf("You inserted an incorrect value. Please insert a number ranging between 1-15cm, and try again.\n");
    while ( (ch=getchar()) != '\n' );
  }
  printf(" Now insert a value for side 'B' of your triangle.\n");
  while(scanf("%d", &sideB) != 1 )
  {
    printf("You inserted an incorrect value. Please insert a number ranging from 1-15cm, and try again.\n");
    while ( (ch=getchar()) != '\n' );
  }
  printf(" And finally, insert a value for side C of your triangle ranging from 1-15cm.\n");
  while(scanf("%d", &sideC) != 1 )
  {
    printf("You inserted an incorrect value. Please insert a number ranging from 1-15cm, and try again.\n");
    while ( (ch=getchar()) != '\n' );
  }
  /*** List of conditions based on user input to identify if the triangle is valid and if so, what type of triangle they get***/
  if(sideA <=0 || sideB<=0 || sideC <=0)
  {
      printf(" You cannot have a triangle with any side having a value of 0.\n");
  }
  else
      if( (sideA+sideB<=sideC) || (sideB+sideC<=sideA) || (sideC+sideA<=sideB) )
      {
          printf("Your triangle is invalid!\n");
          printf("The sum of every pair of sides must be greater than the third side of a triangle.!\n");
          printf("Please try again.\n");
      }
      else
           if( (sideA==sideC && sideB==sideC) || (sideB==sideA && sideC==sideA) || (sideC==sideB && sideA==sideB) ) /*** Code to determine EQUILATERAL TRIANGLE***/
           {
              printf(" Your input creates a valid EQUILATERAL triangle.\n");
           }
           else 
               if( (sideA == sideB ) || (sideB == sideC ) || (sideC == sideA ) )/*** Code to determine ISOSCELES TRIANGLE***/
               {
                   printf("Your input creates a valid ISOSCELES triangle.\n");
               }
               else
                   if( (sideA!= sideB) && (sideB != sideC) )/*** Code to determine SCALENE triangle ***/
                   {
                       printf("Your input creates a valid SCALENE triangle.\n");
                   }
                   else
                   {
                       printf("You have inserted invalid range of values, as a result your triangle is invalid.\n");
                       printf("Please restart the program by closing it and opening it again to retry\n.");
                       printf("Goodbye.\n");
                   }
return(0);
}
下图是编译和运行程序时的结果.
 
                 
基本上,无论我是插入有效整数还是无效整数,它仍然会提供与屏幕截图相同的结果.
我还在学习C所以我可能会在这里和那里错过一些东西,所以任何帮助都非常感激.
此外,我希望能够添加条件while循环,以阻止用户插入不正确的值或在需要整数时插入字符.我会在哪里插入while循环,我将如何编写它?
更新1.我已更新代码部分
 if(sideA <=0 || sideB <=0 || sideC <=0)
  {
      printf(" You cannot have a triangle with any side having a value of 0.\n");
  }
  else 
      if(sideA >15 || sideB >15 || sideC >15)

所有回复都指导我同样的问题并回答.我已经分别更新了代码,它在第二个屏幕截图中看起来非常精细.
我还测试了每个三角形的值,它的工作原理非常好.
还有另外一件事我遇到过.当测试插入除整数之外的值时,它会出现类似下面的屏幕截图.

我知道我必须插入一个条件来阻止某人插入除整数以外的任何东西,但这就是我有点绊倒的地方.如果没有while循环函数,我不太清楚如何做到这一点.有任何想法吗?
更新2:我已经更新了我的代码,以便浮点数可以被视为有效,因为三角形可以有一个浮点整数.然而..

通过这样做,该while代码循环条件仍使语句出现要求用户输入其值后立即sideB和sideC.任何想法如何解决?
也
当测试只插入一个null值或按下回车时,程序没有任何反应.我们怎样才能使用户不能拥有null价值?
更新3:调试后我发现更新2后的问题.我刚刚更改%d为%f`并修复了该问题.仍在使用空值或不允许输入空格键来解决问题.如果有人对此有所了解.请告诉我
Run Code Online (Sandbox Code Playgroud)if(sideA || sideB || sideC <=0)
这不会测试你的想法.它被解析为
if ( (sideA) || (sideB) || (sideC <= 0) )
所以sideA如果sideB是非零,或者如果非零,或者是否为零,则测试为真sideC.
如果您习惯使用布尔类型的其他编程语言,您可能会认为表达式sideA不是布尔值,因此这应该触发类型错误.但是C有效地将整数视为布尔值:C中的布尔值实际上是一个整数值,它是0(表示假)或1(表示真).例如,sideC <= 0如果sideC小于0 则值为1,否则为0.的值a || b是0,如果a和b都为0,否则为1.
C没有任何内置构造来测试三个值中的一个是否为负数.最明确的表达方式是
if (sideA <= 0 || sideB <= 0 || sideC <= 0)
你在其他if陈述中犯了类似的错误.