2015-02-09 61 views
0

我需要創建一個文本字段,可以得到這樣的多輸入...計數和一個文本字段讀取多個輸入

1,2,3,4

然後輸出應該是...

編號輸入的:4

平均數:2.5

問題是如何計算輸入的數量,我的意思是程序如何知道用戶輸入到文本字段中的輸入數量,並使用每個值來計算所有輸入的總和。有人有任何方法或任何想法?

謝謝。

+3

'爆炸()' - '計數()' – 2015-02-09 16:51:17

回答

0

使用explode();並將所有逗號(,)分解。這會給你一個數組。

從那裏,使用計數和循環進行計數並得到平均值。使用trim() aswel來擺脫空的空間。

0

您可以測試這裏的代碼:http://writecodeonline.com/php/

$input = "1, 2, 3, 4"; 

//remove all whitespace 
$input = str_replace(' ', '', $input); 

//turn the string into an array of integers 
$numbers= array_map('intval', explode(',', $input)); 

//the number of elements 
$count = count($numbers); 

//sum the numbers 
$sum = array_sum($numbers); 

//print the number of inputs, a new line character, and the mean 
echo "Number of inputs: $count\r\n"; 
echo 'Mean: ' . $sum/$count; 
相關問題