PHP程序补全求助:计算用户输入数的各类求和并以零终止
Hey there! Let's work through fixing your PHP code for this sum calculation problem. First, let's address a bug in your current input reading logic, then build out the rest of the functionality to get those three sums you need.
First: Fix the Input Reading Bug
Right now, your code calls fgets(STDIN) twice per loop—once to check if the input is 0, and once to add to the array. That means you're actually skipping every other input value! For example, if you type 1, then 2, then 0, your code will read 1 in the while check, then read 2 to push to the array, then check the next input (0) and exit—so 1 never gets added to the array. Oops!
Let's fix that by storing the input value in a variable first, so we can use it both for checking the termination condition and adding to the array:
$nums = array(); while (true) { // Read input once and trim whitespace $input = trim(fgets(STDIN)); // Convert input to integer since we're dealing with numeric values $num = (int)$input; // Exit loop immediately if 0 is entered if ($num === 0) { break; } array_push($nums, $num); }
Second: Calculate the Three Required Sums
Now that we have all valid numbers stored in the $nums array, we can initialize three variables to hold our sums, then loop through each number to categorize and add to the correct total:
// Start all sums at 0 $negativeSum = 0; $positiveEvenSum = 0; $positiveOddSum = 0; foreach ($nums as $num) { if ($num < 0) { // Add to negative sum if number is less than 0 $negativeSum += $num; } elseif ($num > 0) { // For positive numbers, check if even or odd if ($num % 2 === 0) { $positiveEvenSum += $num; } else { $positiveOddSum += $num; } } // We can ignore 0 here since we already stopped input when 0 was entered }
Third: Output the Results
Finally, let's print out the sums so you can see the final calculations:
echo "Sum of negative numbers: " . $negativeSum . "\n"; echo "Sum of positive even numbers: " . $positiveEvenSum . "\n"; echo "Sum of positive odd numbers: " . $positiveOddSum . "\n";
Putting It All Together
Here's the full working code:
$nums = array(); while (true) { $input = trim(fgets(STDIN)); $num = (int)$input; if ($num === 0) { break; } array_push($nums, $num); } $negativeSum = 0; $positiveEvenSum = 0; $positiveOddSum = 0; foreach ($nums as $num) { if ($num < 0) { $negativeSum += $num; } elseif ($num > 0) { if ($num % 2 === 0) { $positiveEvenSum += $num; } else { $positiveOddSum += $num; } } } echo "Sum of negatives: " . $negativeSum . "\n"; echo "Sum of positive evens: " . $positiveEvenSum . "\n"; echo "Sum of positive odds: " . $positiveOddSum . "\n";
Bonus: Optimize by Calculating Sums On-the-Fly
You don't even need to store all numbers in an array if you don't want to—you can calculate the sums as you read each input, which saves memory (especially for large sets of numbers):
$negativeSum = 0; $positiveEvenSum = 0; $positiveOddSum = 0; while (true) { $input = trim(fgets(STDIN)); $num = (int)$input; if ($num === 0) { break; } if ($num < 0) { $negativeSum += $num; } elseif ($num > 0) { $num % 2 === 0 ? $positiveEvenSum += $num : $positiveOddSum += $num; } } echo "Sum of negatives: " . $negativeSum . "\n"; echo "Sum of positive evens: " . $positiveEvenSum . "\n"; echo "Sum of positive odds: " . $positiveOddSum . "\n";
That's it! This code will correctly read input until 0 is entered, then compute and display the three required sums.
内容的提问来源于stack exchange,提问作者Joshua Melvin Benhar

