【发布时间】:2015-03-22 15:58:50
【问题描述】:
我正在编写一个程序,它可以通过插入 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` 并解决了该问题。仍在解决空值问题或不允许输入空格键。如果有人对此有想法。请告诉我
【问题讨论】:
-
条件应该是
if (sideA <= 0 || sideB <= 0 || sideC <= 0) ...。每个条件都必须明确给出。条件(sideA || sideB || sideC <= 0)是正确的 C,但它并没有按照您的想法执行:如果sideA或sideB具有非零值(它们具有)或sideC非零,则为真积极的。 -
你尝试过单步调试代码吗?也许你有一些意想不到的输入?
-
你所有的条件都没有按照你的想法做
-
这个
if(sideA || sideB || sideC <=0)相当于if(sideA!=0 || sideB!=0 || sideC <=0)。这就是问题所在。
标签: c if-statement boolean-logic