Back to Challenge Set 13

Code Challenge: Safe Lookup

Difficulty  

divider

The Brief

What they asked for
Eight prize boxes, and each holds a number of points. Let a
player pick a box and tell them what is inside. Players type
whatever they like, so it must not fall over when they pick
a box that is not there.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.


Notes

  • Start with the points already in an array: 50, 10, 0, 25, 100, 5, 0, 75.
  • Decide whether the player numbers boxes from 1 or from 0, and write the decision in your requirements list. Both are defensible; only one matches how people count.
  • A box that is not there must get a sensible message, not a crash. “Not there” has two ends — Activity 1.24 checked both.
  • Your prompt and your message should both get the range from the array, so adding a ninth box changes them without being edited.
  • A typed word, like abc, will still crash at int.Parse. That is fine for this challenge; numbers out of range are the job.

Sample Output

Sample Output
Pick a box from 1 to 8: 5
Box 5 holds 100 points.
Pick a box from 1 to 8: 9
There is no box 9. The boxes are 1 to 8.

Two real runs. Boxes 0, 9, and −2 all get the message; boxes 1 and 8 are both found.


Submit

Your requirements list and your program file, named cc-safe-lookup.

Commence Challenge