Hello pcoady,
First, please edit the title of your post to remove the <p> tags.
Now to the assignment. Two preliminary matters:
Your specification says: “until a negative number is entered.” But you are testing on only one negative number, namely -1. What if the user enters -2? You can fix this by changing the while condition to:
while ( $number >= 0 )
The closing brace is missing from the final else block.
How to get the script to record the largest and smallest numbers? As an alternative to the solution proposed by Loops, you can treat the first user-entered number differently from all the others:
while ($number >= 0) { $total += $number; $howmany++; if ($howmany == 1) { $largest = $number; $smallest = $number; } else { $largest = $number if $number > $largest; $smallest = $number if $number < $smallest; } print "Please enter a number or -1 to end: "; chomp($number = <>); }
— a variation on the solution given by Anonymous Monk.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re: I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated.
by Athanasius
in thread I am stuck on how to print the largest and smallest numbers from user inputted values. Any help is appreciated.
by pcoady
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |