Lecture 04b - Making Decisions
Lecture 04b - Making Decisions
Making decisions
Lecturer:
Ms Marinda Taljaard / Mr Ighsaan Salie / Mr Ntembeko Jafta
1
2024/03/07
Homework
Write a program named GuessingGame that generates a random number
between 1 and 10. Ask a user to guess the random number, then display the random
number and a message indicating whether the user’s guess was too high, too
low, or correct.
You can create a random number that is at least min but less than max using the
following statements:
Random ranNumberGenerator = new Random();
int randomNumber;
randomNumber = ranNumberGenerator.Next(min,max);
2
2024/03/07
3
2024/03/07
4
2024/03/07
5
2024/03/07
Class exercise 1
Write a program that requests from a user the number of pies (at R18.50 per pie) and
the number of hamburgers (at R35.00 per hamburger), and then calculates the total for
the order.
6
2024/03/07
Class exercise 2
People who earn less than R6 000.00 do not pay tax. People older than 70 years
do not pay tax. Write a program that reads in a person’s salary and age. If the
person should pay tax, your program should request the tax percentage. The
program must finally display an appropriate message.
▪ Declare variables, constants
▪ Prompt the user for the values
▪ Choice to be made - determine the condition(s) to check
▪ Display result
(ideally use appropriate formatting)
7
2024/03/07
8
2024/03/07
9
2024/03/07
Initialising variables
▪ When a variable gets a
value inside an if / if-else
• Declare the variable before
the if
• Instead of when you want to
assign a value Solution partially hidden,
as you should be
• As message was declared completing it yourself
inside the if, it is not
recognised outside the if
10
2024/03/07
Formatting output
▪ Formatting
• If no specific instructions are given – make appropriate choices
- Monetary amounts should be formatted to show the currency sign
• Output should look good
11
2024/03/07
Home work
▪ Write a program that can be used to teach a user the word for the numbers 1 –
10 in a different language (e.g. Spanish)
▪ The Spanish words are:
• Uno, dos, tres, cuatro, cinco, seis, siete, ocho, nueve,
diez
▪ Your program should request an integer from the user, and the display the
corresponding word in Spanish, if the user enters a number that is not in the range
of 1 – 10, display an appropriate message
▪ Make use of a switch statement in your solution
12
2024/03/07
Any questions
13
2024/03/07
Last slide
▪ Time to pack up and go
▪ See you another time
▪ ☺
14