How to count min,max in loop (java) [closed]

2019-09-28 23:19发布

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 ?

3条回答
等我变得足够好
2楼-- · 2019-09-28 23:48

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

查看更多
我只想做你的唯一
3楼-- · 2019-09-28 23:55
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
}
查看更多
贼婆χ
4楼-- · 2019-09-28 23:55

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.

查看更多
登录 后发表回答