问题
I have problem with coding 1 app for toomorow lectures.
So, That program ask user for numbers, on typed "100" it's stops, and it shows: -average -min -max
I know what kind of loop, i need to use (do-while right?). But how i'm suppoused to count average,min,max without using arrays ?
回答1:
this is shorthand pseudo code because you have to code this yourself:
min = max = total = user_input
count = 0
do {
count++
input = get_input
total += input
min or max maybe = input
} while (input !=100)
average = total / count
print stuff
good luck
回答2:
Step 1 : user three temp variable
Step 2 : initialize three temp variable(min,max,avg all by 0)
Step 1 : inside the loop if temp > max ==>> max = temp
Step 1 : inside the loop if temp < min ==>> min = temp
Step 1 : avg = ( avg + temp )/i
int[] values = { .. values .. }
int min=0,max=0,avg = 0;
for(int i=1;i<=values.length;i++)
{
if(min > values[i])
min = values[i]
if(max < values[i])
max = values[i]
avg = ( avg + values[i] ) / i
}
回答3:
I can tell you how to calculate average min and max without arrays. I cannot, however tell you how to calculate average min and max WITH arrays.
Minimum is easy:
int current_min
ArrayList<int> find_min = new ArrayList<int>();
for (int c : find_min)
if (c < current_min)
current_min = c;
Maximum is a little bit harder. You need to use "functions"
boolean check_if_integer_is_bigger_than_another_integer(int another_integer_to_check_against, int the_original_integer_goes_here_into_this_argument_here)
{
if (another_integer_to_check_against > the_original_integer_goes_here_into_this_argument_here)
return (another_integer_to_check_against > the_original_integer_goes_here_into_this_argument_here) //Important
return (another_integer_to_check_against > the_original_integer_goes_here_into_this_argument_here)
}
int current_max
ArrayList<int> find_max = new ArrayList<int>();
for (int c : find_max)
if (check_if_integer_is_bigger_than_another_integer(c, current_max))
current_max = c;
I dont even want to go into averaging. You have to add numbers and I'm not exactly qualified for that.
来源:https://stackoverflow.com/questions/13684973/how-to-count-min-max-in-loop-java