Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/468.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在javascript中使用while循环为用户输入重新编程_Javascript_While Loop_Prompt - Fatal编程技术网

在javascript中使用while循环为用户输入重新编程

在javascript中使用while循环为用户输入重新编程,javascript,while-loop,prompt,Javascript,While Loop,Prompt,我不熟悉编码和尝试一些作业。我试图提示用户输入三角形边的长度。程序计算面积并返回一个值。如果给定的长度不能创建三角形,我尝试使用while循环来重新提示用户 While (true) { SideA = parseFloat(prompt("Enter the length of Side A")); SideB = parseFloat(prompt("Enter the length of Side B")); SideC = parseFloat(prompt("E

我不熟悉编码和尝试一些作业。我试图提示用户输入三角形边的长度。程序计算面积并返回一个值。如果给定的长度不能创建三角形,我尝试使用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);

您可以使用do while循环来实现这一点:

function checkTriangleInequality(a, b, c) {
   return a < b + c && b < c + a && c < b + a;
}

function promptSides() {
  let a, b, c;
   while (true) {
    a = parseFloat(prompt("Enter a"));
    b = parseFloat(prompt("Enter b"));
    c = parseFloat(prompt("Enter c"));

    if (!checkTriangleInequality(a, b, c)) {
      alert("Input is invalid, please try again.")
    } else {
      let s = (a+b+c)/2
      let area = parseFloat(Math.sqrt((s*(s-a)*(s-b)*(s-c))));
      console.log(area);
      break;
    }
   }
} 
功能检查三角线质量(a、b、c){
返回a
到目前为止您尝试了什么?
而(!isValidTriangle(一,二,三)){prompt(“输入长度”)}
。嘿,我重新提交了我的问题并包含了我到目前为止的代码。抱歉,我在格式化方面遇到了麻烦。第一次海报。谢谢!我这一部分做得很顺利。哪里是输入警告并告知用户输入无效并重新输入的理想位置?我不希望它在第一次运行时出现,因此我不确定如何使用此结构使其工作。