Spaces:
Running
Running
Create 10b. Print ticket price
Browse files
Week 1: Types, condition clauses and loops/10b. Print ticket price
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
'''
|
| 2 |
+
The program comes with a predefined code to read the age of the user. Your task is to print the ticket price on the screen according to the following rules:
|
| 3 |
+
|
| 4 |
+
0 - 9 years old: 2 euros
|
| 5 |
+
10 to 64 years old: 5 euros
|
| 6 |
+
over 65: free of charge
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
If the user enters a negative number, a message will be printed indicating an incorrect input. See the three example printouts below for a sample of the printouts:
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
|
| 13 |
+
Example 1:
|
| 14 |
+
Give an age: 7
|
| 15 |
+
Ticket price is 2 euros
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
Example 2:
|
| 19 |
+
Give an age: 94
|
| 20 |
+
The ticket is free
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
Example 3:
|
| 24 |
+
Give an age: -1
|
| 25 |
+
The input is unviable
|
| 26 |
+
'''
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
import java.util.Random;
|
| 30 |
+
import java.util.Scanner;
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
public class Test{
|
| 35 |
+
public static void main(String[] args){
|
| 36 |
+
final Random r = new Random();
|
| 37 |
+
|
| 38 |
+
Scanner lukija = new Scanner(System.in);
|
| 39 |
+
System.out.print("Give an age: ");
|
| 40 |
+
int age = Integer.valueOf(lukija.nextLine());
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
if (age <0) {
|
| 44 |
+
System.out.println("The input is unviable");
|
| 45 |
+
}
|
| 46 |
+
else if (age <10) {
|
| 47 |
+
System.out.println("Ticket price is 2 euros");
|
| 48 |
+
}
|
| 49 |
+
else if (age <65) {
|
| 50 |
+
System.out.println("Ticket price is 5 euros");
|
| 51 |
+
}
|
| 52 |
+
else {
|
| 53 |
+
System.out.println("The ticket is free");
|
| 54 |
+
}
|
| 55 |
+
}
|
| 56 |
+
}
|