2012-09-19 21 views
1

我正在写一个基本的程序在JavaScript中,它决定了如果有人将某个变量输入到3个输入中,会产生什么样的三角形。Javascript三角确定

<html> 
<head> 
<link href="stylesheet/stylesheet.css" rel="stylesheet" type="text/css"> 

<script language="Javascript" type="text/javascript"> 

/* Key/Legend 
Var 
    inp1 = input1 
    inp2 = input2 
    inp3 = input3 
    Triangle_Inputs = Form Name 

    */ 

/* Notes 

    In computing, a parser is one of the components in an interpreter or 
    compiler that checks for correct syntax and builds a data structure 
    (often some kind of parse tree, abstract syntax tree or other hierarchical structure) 
    implicit in the input tokens. 


    Technique 
    if (side1 is equal to side2 AND side 2 is equal to side3) {equalitateral} 

    if (side1 is equal to side2 AND side 2 doesn't equal to side3) {isosceles} 

    if (side1 doesn't equal to side2 AND side2 doesn't equal to side 3 AND side 3 doesn't equal side 1) {scalene} 

    http://www.w3schools.com/js/js_comparisons.asp 

    */ 


function checkinputs() 
{ 
/* Var = parseInt(document.Name_Of_Element_Form.Field_Name(Input).value); */ 
/* Input Fields */ 
inp1 = parseInt(document.Triangle_Inputs.input1.value); 
inp2 = parseInt(document.Triangle_Inputs.input2.value); 
inp3 = parseInt(document.Triangle_Inputs.input3.value); 
/* Side options */ 
sideA = (inp1 + inp2); 
sideB = (inp1 + inp3); 
sideC = (inp2 + inp3); 
    if (sideA == sideB && sideB == sideC) { 
    alert("Equalateral"); 
    } 
    if (sideA == sideB && != sideC) { 
    alert("Isosceles"); 
    } 
    if (sideA != sideB == sideC) { 
    alert("Isosceles"); 
    } 
    if (sideA != sideB != sideC != sideA) { 
    alert("Scalene!"); 
    } 
} 
</script> 


</head> 
<body> 
<div id="Container"> 

<div id="Header"><h1></h1></div> 

     <div id="Content_1"> 
       <div id="Explanation"> 
       This calculator will determine what 
       triangle you have made depending on 
       the integer values in the input fields. 

       </div> 
       <div id="Form"> 
        <FORM NAME="Triangle_Inputs" METHOD="GET"> 
        Enter the triangle values below: <br> 
        <p> 
        <h4>Side 1: </h4><BR> 
        <INPUT TYPE="Integer" NAME="input1" VALUE=""><P> 
        <h4>Side 2: </h4><BR> 
        <INPUT TYPE="Integer" NAME="input2" VALUE=""><P> 
        <h4>Side 3:</h4> <BR> 
        <INPUT TYPE="Integer" NAME="input3" VALUE=""><P> 
        <INPUT TYPE="button" NAME="Submit" Value="Submit" Class="Submit" onClick="checkinputs()"> 
        </FORM> 
       </div> 
       <div id="Verbal_Output"> 
        <h2>You made a:</h2> 
        <p>   
        <h2>Triangle</h2> 
       </div> 

      </div> 
      <p> 
      <p> 
     <div id="Content_2"> 

     <div id="Image_Output">asdad</div> 
    </div>  
</div> 



</body> 
</html> 

但是我认为我错过了一些东西,我似乎无法得到警报来告诉用户任何东西。

谢谢大家,对不起张贴这个问题,它真的很困惑我

+1

您不能像这样链接逻辑比较 - 像'(sideA == sideB)== sideC'那样进行解析,它将真/假与第三方进行比较,并且不太可能是真实的。尝试类似'sideA == sideB && sideB == sideC'。 – DCoder

+0

你好,感谢您的快速回复。我现在只是在尝试。 这是否意味着对于等腰将是 if(sideA == sideB &&!= sideC) –

回答

0
  1. 你永远调用checkinputs功能,所以形式永远不会转化成SIDEA,B,C。

  2. 您在equation函数中缺少a}。请记住正确缩进代码以发现这些错误。

+0

非常感谢您的回复,我将如何避免使用checkinput?这是在功能本身还是将是一个单独的功能。 此外,我刚刚编辑公式,感谢您发现失踪} –

+0

很多方式来处理。你可以从方程式()中调用它,或者你可以创建一个调用它们的新函数。 – sphair

+0

我刚刚编辑了上面的代码。 所以内函数方程式(){在这里调用它} –

0

你需要产业链的比较与逻辑运算符在一起,例如:

sideA == sideB && sideB == sideC 

,而不是

sideA == sideB == sideC 
2

尝试使用此功能:

function getTriangleType(a,b,c) { 
    return (a === b && b === c) && 'equilateral' || 
    (a === b || a === c || b === c) && 'isosceles' || 
    'scalene'; 
} 

演示:http://jsbin.com/eyoxor/3/edit

+0

感谢您的回应,它揭示了我将如何去做我的程序。然而,我现在正在使用其他方法:) 非常感谢你 –

+0

我的观点是你不需要检查'scalene',如果其他类型按照这个顺序进行评估... – elclanrs

+0

谢谢你的回复elclanrs :)一旦我真的得到它的工作,我会试验你的方法 –