在伪代码中使用循环

问题描述 投票:0回答:1

我一直在创建一个程序来确定粉刷一个房间所需的油漆罐数量。我的教授让我们为程序编写伪代码,现在要我们通过输入不同类型的循环来修改代码,例如 WHILE、DO…WHILE 和 FOR…NEXT。

我必须在我的项目伪代码中的某处包含三种不同循环结构中的两种(while、do…while、for…next)。

这就是我当前的伪代码的样子,但我不知道哪两个循环最有效,也不知道如何将它们输入到已经存在的伪代码中。

Start

Display "Enter room length"

Input roomLength

Display "Enter room width"

Input roomWidth

Display "Enter room height"

Input roomHeight

Display "Enter number of doors"

Inputs doors

Display "Enter number of windows"

Input windows

Set paintCans = (L x W x H) - (20 sq. ft. per door & 15 sq. ft. per window) / (400 sq. ft. per gallon)

Display "Total cans of paint is", + paintCans

End
loops for-loop while-loop do-while pseudocode
1个回答
0
投票

输入门

// 使用 WHILE 循环来验证门的输入 同时门< 0 Display "Invalid input. Please enter a positive number of doors:" Input doors End While

显示“输入窗口数”

输入窗口

// 使用 DO...WHILE 循环来验证窗口的输入

如果窗户< 0 Then Display "Invalid input. Please enter a positive number of windows:" Input windows End If Loop While窗户< 0

说明: 在这个修改后的伪代码中,使用一个 WHILE 循环来确保用户输入一个正数作为门的数量。如果用户输入负数或零,循环将继续提示用户,直到输入正数。

DO...WHILE 循环用于确保用户输入的窗口数为正数。循环将提示用户输入至少一次,并将继续这样做直到输入正数。

验证输入后,可涂漆面积和所需油漆罐数如前所述计算,并显示结果。

© www.soinside.com 2019 - 2024. All rights reserved.