'F' → Fullscreen
Everything today goes in a new program named 1-17-lunar-lander.
How to read the code blocks. Each task's code block shows your whole program, from line 1, as it should look when that task is finished.
The green lines marked with a + are the ones you add or change for that task — everything else is already in your file. Do not type the + sign.
Where a block shows only comments, you write that code yourself. The comments stay in your file.
If a program will not stop, press Ctrl + C in the terminal. You will need this today. It is not an error and nothing is lost.
Console.WriteLine("=== LUNAR LANDER ===");Console.WriteLine("Touch down at a speed of 5 or less.");Console.WriteLine();
int altitude = 100;int speed = 0;int turn = 0;
while (altitude > 0){ turn = turn + 1; Console.WriteLine($"Turn {turn}: altitude {altitude}, speed {speed}");
speed = speed + 4; altitude = altitude - speed;}
Console.WriteLine();Console.WriteLine($"Touchdown at speed {speed}.");
if (speed <= 5){ Console.WriteLine("The Eagle has landed.");}else{ Console.WriteLine("CRASH. The lander is in pieces.");}if stops you burning fuel you do not have. With an empty tank, you are back in free fall.while loop is for.Console.WriteLine("=== LUNAR LANDER ===");Console.WriteLine("Touch down at a speed of 5 or less.");Console.WriteLine();
int altitude = 100;int speed = 0;int fuel = 40;int turn = 0;
while (altitude > 0){ turn = turn + 1; Console.WriteLine($"Turn {turn}: altitude {altitude}, speed {speed}, fuel {fuel}");
Console.Write("Burn? "); int burn = int.Parse(Console.ReadLine());
if (burn > fuel) { burn = fuel; }
fuel = fuel - burn; speed = speed + 4 - burn; altitude = altitude - speed;}
Console.WriteLine();Console.WriteLine($"Touchdown at speed {speed}.");
if (speed <= 5){ Console.WriteLine("The Eagle has landed.");}else{ Console.WriteLine("CRASH. The lander is in pieces.");}Console.WriteLine("=== LUNAR LANDER ===");Console.WriteLine("Touch down at a speed of 5 or less.");Console.WriteLine();
int altitude = 100;int speed = 0;int fuel = 40;int turn = 0;
// Ask for a difficulty from 1 to 3 and convert it// While it is below 1 or above 3, say it is invalid and ask again// Easy (1) gets 60 fuel, Normal (2) keeps 40, Hard (3) gets 30
while (altitude > 0){ turn = turn + 1; Console.WriteLine($"Turn {turn}: altitude {altitude}, speed {speed}, fuel {fuel}");
Console.Write("Burn? "); int burn = int.Parse(Console.ReadLine());
if (burn > fuel) { burn = fuel; }
fuel = fuel - burn; speed = speed + 4 - burn; altitude = altitude - speed;}
Console.WriteLine();Console.WriteLine($"Touchdown at speed {speed}.");
if (speed <= 5){ Console.WriteLine("The Eagle has landed.");}else{ Console.WriteLine("CRASH. The lander is in pieces.");}Two changes, one at a time. Write down exactly what happens, and put each back before the next.
altitude. Run it and keep entering burns for a few turns, then press Ctrl + C.abc instead of a number.For number 1, record what the turns showed and how you stopped it. For number 2, record the exception and the line it names.
Number 2 is not a loop bug, and your difficulty loop did not catch it. Say why a loop that checks whether the number is between 1 and 3 never gets the chance to look at abc.
int.Parse that reports failure instead of crashing: int.TryParse(Console.ReadLine(), out difficulty) is true when the text was a number and false when it was not.abc is refused exactly like 9 and 0 were, instead of ending the program.int difficulty; on its own line first, with no value.abc, and why that is better than the exception even though both stop the wrong value getting through.int count = 5;
while (count > 0){ Console.WriteLine(count); count = count - 1;}count ← 5
REPEAT UNTIL(count ≤ 0){ DISPLAY(count) count ← count - 1}Read those two conditions again. They are opposites. while keeps going while its condition is true. REPEAT UNTIL keeps going until its condition becomes true — so it runs while that condition is false.
count > 0 and count ≤ 0 describe the same loop from the two ends. Translating one into the other means flipping the comparison, not copying it across.
| C# | Exam notation |
|---|---|
while (count > 0) | REPEAT UNTIL(count ≤ 0) |
while (fuel < 500) | REPEAT UNTIL(fuel ≥ 500) |
while (playing) | REPEAT UNTIL(NOT playing) |
The reference sheet also has a counted loop, REPEAT n TIMES, which runs a block a fixed number of times with no condition at all. C# writes that one with for, and that is Activity 1.19.
Your finished program should run like this.
=== LUNAR LANDER ===Touch down at a speed of 5 or less.
Difficulty (1-3)? 9 [Enter]Invalid. Enter 1, 2, or 3.Difficulty (1-3)? 2 [Enter]Turn 1: altitude 100, speed 0, fuel 40Burn? 0 [Enter]Turn 2: altitude 96, speed 4, fuel 40Burn? 0 [Enter]Turn 3: altitude 88, speed 8, fuel 40Burn? 0 [Enter]Turn 4: altitude 76, speed 12, fuel 40Burn? 4 [Enter]Turn 5: altitude 64, speed 12, fuel 36Burn? 4 [Enter]Turn 6: altitude 52, speed 12, fuel 32Burn? 4 [Enter]Turn 7: altitude 40, speed 12, fuel 28Burn? 4 [Enter]Turn 8: altitude 28, speed 12, fuel 24Burn? 4 [Enter]Turn 9: altitude 16, speed 12, fuel 20Burn? 4 [Enter]Turn 10: altitude 4, speed 12, fuel 16Burn? 11 [Enter]
Touchdown at speed 5.The Eagle has landed.abc producedAnswer the following questions before submitting your work.
Submit four things to the dropbox:
abc.