简体   繁体   English

伪代码输入年龄,计算Java的最大值和平均值

[英]pseudocode input age, calculate max and average for java

Write a pseudo code algorithm which will input a series of people's ages (as integers between 1 and 120 inclusive) and calculate the maximum and average age. 编写一个伪代码算法,该算法将输入一系列人的年龄(为1到120之间的整数),并计算最大和平均年龄。 The average age should be calculated as a real number. 平均年龄应以实数计算。 Your algorithm should continue to input age values until the user inputs a value of zero. 您的算法应继续输入年龄值,直到用户输入零值为止。 This is a signal for the algorithm to stop inputting the age, and to then output the average and maximum ages. 这是算法停止输入年龄,然后输出平均年龄和最大年龄的信号。 If the user enters an age that is invalid then your algorithm should continue to re-prompt the user until they enter a valid age. 如果用户输入的年龄无效,则您的算法应继续提示用户,直到他们输入有效年龄为止。 Your algorithm should make good use of sub modules. 您的算法应充分利用子模块。 Note zero should not be included when determining the average or maximum age. 注意确定平均或最大年龄时不应包括零。

this is my practice question 这是我的练习题

i have come up with 我想出了

main 

FOR 
age = inputAge <-prompt "enter age"
max = getMax <- age
sum = sum + age
average = getAverage <- sum, number of times input
END FOR
OUTPUT max, average

END MAIN

METHOD inputAge
IMPORT prompt
EXPORT age
    INPUT age
    WHILE age >= 120 AND age <= 0 DO
        OUTPUT "enter valid age"
        INPUT age

    END WHILE 
END 

METHOD getMax
IMPORT age
EXPORT max
    IF max < 0 THEN
    max = age
END

METHOD getAverage
IMPORT sum, number of times input
EXPORT average
    average = sum / number of times input
END

my problem is that im not quite sure how to input 1 to 120 and stop loop when 0 is input and getting number of times input so i can calculate the average. 我的问题是,我不太确定如何输入1到120,并在输入0时停止循环并获得输入次数,因此我可以计算平均值。 the for loop in the main is also a problem. 主要的for循环也是一个问题。 i get what the question is asking but its just not getting together in my head. 我明白了这个问题要问的问题,但只是头脑不明白而已。 could i get help with this question? 我可以在这个问题上得到帮助吗?

Well, for starters, it would be far simpler to calculate the average after collecting all inputs and saving the biggest value until then as the maximum value. 好吧,对于初学者而言,在收集所有输入并将最大值保存为最大值之后,计算平均值将容易得多。 So, setting the sum and the max to 0 and creating a variable to count the number of inputs would be a nice start. 因此,将总和和最大值设置为0并创建一个变量以计算输入数量将是一个不错的开始。 I would do something like this: 我会做这样的事情:

main 

max = 0
sum = 0
count = 0

WHILE true 
age = inputAge <-prompt "enter age"
IF age == 0 THEN
    BREAK
END IF
IF age > max THEN
    max = age
END IF
sum = sum + age
count = count + 1
END WHILE
average = getAverage <- sum, count
IF count == 0 THEN
    OUTPUT "There was no input"
ELSE
    OUTPUT max, average
END IF

END MAIN

METHOD inputAge
IMPORT prompt
EXPORT age
    INPUT age
    WHILE age >= 120 AND age <= 0 DO
        OUTPUT "enter valid age"
        INPUT age

    END WHILE 
END

METHOD getAverage
IMPORT sum, number of times input
EXPORT average
    average = sum / number of times input
END

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM