You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于while循环的未来五年学期学费预测程序设计咨询

Calculating Tuition Increases with a While Loop

Hey there! Let's work through this problem step by step—converting your for loop approach to a while loop that shows the 5-year tuition projections.

First, let's recap the requirements clearly:

  • Starting tuition: $6000
  • Annual increase: 2% (so each year's tuition is 102% of the previous year's)
  • We need to display the projected tuition for each of the next 5 years using a while loop.

Pseudo-Code Implementation

Since you started with a module-based pseudo-code structure, here's how to adapt it to use a while loop:

Module main()
    // Initialize core variables
    Declare Real tuition = 6000.0
    Declare Integer year_counter = 1
    Declare Integer total_years = 5

    // While loop to iterate through each future year
    While year_counter <= total_years
        // Display the current year's tuition (formatted to 2 decimal places for currency)
        Display "Year ", year_counter, ": $", Format(tuition, "0.00")
        
        // Calculate next year's tuition (2% increase)
        tuition = tuition * 1.02
        
        // Increment the counter to move to the next year
        year_counter = year_counter + 1
    End While
End Module

Breakdown of the Logic

  • We start with the initial tuition ($6000) and a counter set to 1 (representing the first future year).
  • The loop runs as long as year_counter is less than or equal to 5—this ensures we get exactly 5 years of projections.
  • In each iteration:
    1. We print the tuition for the current year.
    2. We update the tuition to reflect the 2% increase for the next year.
    3. We increment the counter to avoid an infinite loop and move to the next year.

Real-World Code Example (Python)

If you want a runnable version, here's how this translates to Python:

# Set up initial values
current_tuition = 6000.0
year = 1
years_to_project = 5

print("5-Year Tuition Projections:")
while year <= years_to_project:
    # Format tuition to 2 decimal places for clean currency display
    print(f"Year {year}: ${current_tuition:.2f}")
    # Apply 2% annual increase
    current_tuition *= 1.02
    # Move to the next year
    year += 1

Output Preview

When you run this, you'll get output like this:

5-Year Tuition Projections:
Year 1: $6000.00
Year 2: $6120.00
Year 3: $6242.40
Year 4: $6367.25
Year 5: $6494.59

Quick note: If you want the first year to be the already increased tuition (i.e., $6120 as Year 1), just swap the order of printing and updating the tuition in the loop. That way, you first calculate the increase, then print the new value.

Key Takeaways

  • Always initialize your loop control variable (year_counter/year) and your target variable (tuition) before the loop.
  • Make sure your loop condition will eventually become false—otherwise, you'll hit an infinite loop!
  • Formatting currency to two decimal places makes the output much more readable for this kind of problem.

内容的提问来源于stack exchange,提问作者jblue

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:54:21