Java Reference
In-Depth Information
public static int random(int m, int n) {
//returns a random integer from m to n, inclusive
return (int) (Math.random() * (n - m + 1)) + m;
} //end random
} //end class GuessTheNumber
Programming note : It is a good idea to remind the user that he has the option of giving up and how to do so.
To this end, the prompt can be as follows:
Your guess (0 to give up)?
6.5 Drills in Addition
We want to write a program to drill a user in simple arithmetic problems (Program P6.3). More specifically, we want
to write a program to create addition problems for a user to solve. The problems will involve the addition of two
numbers. But where do the numbers come from? We will let the computer “think” of the two numbers. By now, you
should know that, in order to do this, the computer will generate two random numbers.
We also need to decide what size of numbers to use in the problems. This will determine, to some extent, how
difficult the problems are going to be. We will use two-digit numbers, that is, numbers from 10 to 99. The program can
be easily modified to handle numbers in a different range.
The program will begin by asking the user how many problems he wants to be given. The user will type the
number required. He will then be asked how many attempts he wants to be given for each problem. He will enter this
number. The program then proceeds to give him the requested number of problems.
The following is a sample run of the program. Underlined items are typed by the user; everything else is typed by
the computer.
Welcome to Problems in Addition
How many problems would you like? 3
Maximum tries per problem? 2
Problem 1, Try 1 of 2
80 + 75 = 155
Correct, well done!
Problem 2, Try 1 of 2
17 + 29 = 36
Incorrect, try again
Problem 2, Try 2 of 2
17 + 29 = 46
Correct, well done!
Problem 3, Try 1 of 2
83 + 87 = 160
Incorrect, try again
Problem 3, Try 2 of 2
83 + 87 = 180
Sorry, answer is 170
Thank you for playing. Bye...
 
Search WWH ::




Custom Search