Difference between revisions of "The Labyrinth"
Line 92: | Line 92: | ||
"Choose your fate: 1. Red dragon 2. Blue dragon."); | "Choose your fate: 1. Red dragon 2. Blue dragon."); | ||
answer = Integer.parseInt(input); | answer = Integer.parseInt(input); | ||
− | ... | + | ... <---The program continues |
} | } | ||
else | else |
Revision as of 12:18, 6 April 2010
Back to the Program-A-Day homepage
ProblemFor this problem, you will create a labyrinth. The user will be given a series of decisions to make in hope of making it to the treasure. For simplicity sake, the labyrinth will very small. The input will be recorded using JOptionPane.showInputDialog and the output will be done with JOptionPane.showMessageDialog. The user will enter an integer based on their choice. If the user makes the wrong choice, the game will be over and the program will end. To complete this problem, you should have a good understanding of Nested if statements as well as JOptionPane Methods. Design the labyrinth exactly as described below:
|
SideSectionTitle
An image or By Students section | |
---|---|---|
SolutionThis problem may seem daunting at first, but once you have the general idea of how it's supposed to work, it becomes much more clear. Like any other program, you must consider three things:
In this program you will do all three things, repeatedly. First of course, you will need variables to store your input: String input; int answer; The input variable will receive the raw text (String) directly from JOptionPane.showInputDialog. The answer variable will store the choice that the user will make as an integer. To begin the program, we will ask the user for their first choice: input = JOptionPane.showInputDialog(null,"You have arrived at the entrance of the labyrinth.\n"+ "Large doors stand in front of you. A wooden sign reads \"Death Awaits\".\n"+ "Choose your fate: 1. Go inside 2. Leave"); There are a couple of things that may seem strange to you. One is that the showInputDialog method spans three lines. This is perfectly fine. In fact, it is preferred so that your string doesn't stretch outside the bounds of your editor, requiring you to scroll to see it. You can split your string into multiple lines in the editor by ending the string with a quotation mark, adding the '+' character, and then continuing the string on the next line.
answer = Integer.parseInt(input); Now that we have received the input and converted it into an integer, we must do something with the input. We must check to see which option the user picked: if(answer == 1) { input = JOptionPane.showInputDialog(null, "You enter the labyrinth, "+ "well aware that your life may be in jeopardy.\nAlmost instantly your suspicions are confirmed. "+ "There are two doors with two dragons guarding them.\nOne dragon is good, one is evil.\n"+ "Choose your fate: 1. Red dragon 2. Blue dragon."); answer = Integer.parseInt(input); ... <---The program continues } else { JOptionPane.showMessageDialog(null,"Before you can leave, a hungry tiger attacks and kills you. Game Over."); } If the user answers 1, the program will continue. We will receive more input from the user to continue the quest. If they answer 2, a message dialog is displayed (rather than an input dialog), and the program ends. | ||
Code |