【发布时间】:2019-10-30 01:47:26
【问题描述】:
我是编码和尝试一些作业的新手。我试图提示用户输入三角形边的长度。程序计算面积并返回一个值。如果给定的长度没有创建三角形,我正在尝试使用 while 循环来重新提示用户。
While (true)
{
SideA = parseFloat(prompt("Enter the length of Side A"));
SideB = parseFloat(prompt("Enter the length of Side B"));
SideC = parseFloat(prompt("Enter the length of Side C"));
//Calculate half the perimeter of the triangle
S = parseFloat(((SideA+SideB+SideC)/2));
//Calculate the area
Area = parseFloat(Math.sqrt((S*(S-SideA)*(S-SideB)*(S-SideC))));
if(isNaN(Area))
{
alert("The given values do not create a triangle. Please re-
enter the side lengths.");
}
else
{
break;
}
}
//Display results
document.write("The area of the triangle is " + Area.toFixed(2) + "."
+ PA);
【问题讨论】:
-
到目前为止你尝试了什么?
-
while(!isValidTriangle(one, two, three)) { prompt("Enter lengths") }. -
嘿,我重新发布了我的问题并包含了我到目前为止的代码。对不起,我在格式上苦苦挣扎。第一次海报。
标签: javascript while-loop prompt