Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Решение със if else:
- import java.util.Scanner;
- public class DayOfWeek {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine());
- if (num == 1) {
- System.out.println("Monday");
- } else if (num == 2) {
- System.out.println("Tuesday");
- } else if (num == 3) {
- System.out.println("Wednesday");
- } else if (num == 4) {
- System.out.println("Thursday");
- } else if (num == 5) {
- System.out.println("Friday");
- } else if (num == 6) {
- System.out.println("Saturday");
- } else if (num == 7) {
- System.out.println("Sunday");
- } else {
- System.out.println("Error");
- }
- }
- }
- Решение със switch:
- import java.util.Scanner;
- public class DayOfWeek {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine());
- switch (num) {
- case 1:
- System.out.println("Monday");
- break;
- case 2:
- System.out.println("Tuesday");
- break;
- case 3:
- System.out.println("Wednesday");
- break;
- case 4:
- System.out.println("Thursday");
- break;
- case 5:
- System.out.println("Friday");
- break;
- case 6:
- System.out.println("Saturday");
- break;
- case 7:
- System.out.println("Sunday");
- break;
- default:
- System.out.println("Error");
- break;
- }
- }
- }
- Решение с тернарен оператор:
- import java.util.Scanner;
- public class DayOfWeek {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine());
- System.out.println(
- num == 1 ? "Monday" :
- num == 2 ? "Tuesday" :
- num == 3 ? "Wednesday" :
- num == 4 ? "Thursday" :
- num == 5 ? "Friday" :
- num == 6 ? "Saturday" :
- num == 7 ? "Sunday" : "Error");
- }
- }
- Решение с масив и тернарен оператор:
- import java.util.Scanner;
- public class DayOfWeek {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num = Integer.parseInt(scanner.nextLine());
- String[] dayOfWeek = new String[]{"Error", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
- System.out.println(dayOfWeek[(num > 0 && num < dayOfWeek.length ? num : 0)]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement