如何在数组中输入5个元素并打印元素总和

时间:2017-06-28 13:47:19

标签: javascript php

我正在尝试在数组中添加5个元素并在没有函数的情况下打印java脚本中的元素总和

  <HTML>
    <body>
         <script type="text/javascript">
    	 var x=new Array(5);
    	 for(i=0;i<x.length;i++){
    	x[i]=prompt("enter Any Value") <!-- its properly working till now -->
    	 
    	 t=0;
    	 for(t+= x[i];) 
    	  
    	 for(i=0;i<x.length;i++){
    	 document.write(x[i]);
    	 }
    	 </script>
    </body>
    </HTML>

3 个答案:

答案 0 :(得分:1)

为什么不试试

Dim i As Integer, j As Integer
Dim sourcesheet As Worksheet, targetsheet As Worksheet

j = 1

Set sourcesheet = Sheets("Strategies")
Set targetsheet = Sheets("Stats")

With sourcesheet   
    For i = 1 To 100
        s = .Cells(i, 6).Value
        If Application.WorksheetFunction.IsText(s) Then
            .Cells(i, 6).Copy targetsheet.Cells(2, j)
            j = j + 1
        End If
    Next i
End With

答案 1 :(得分:1)

试试这个:

    <HTML>
<body>
     <script type="text/javascript">
     var x=new Array(5);
     t=0;
     for(i=0;i<x.length;i++){
        x[i]=prompt("enter Any Value") <!-- its properly working till now -->
        t+= x[i]; 
     }
     document.write(t);
     </script>
</body>
</HTML>

答案 2 :(得分:1)

更正了您的代码:

<HTML>
<body>
     <script type="text/javascript">

     var x = new Array(5);
     var sum = 0;

     for(i=0;i < x.length;i++){
        x[i] = parseInt(prompt("enter Any Value")); <!-- its properly working till now -->
        sum += x[i];
    }
    alert("Sum is: " + sum);


     </script>
</body>

相关问题