Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- РЕШЕНИЕ С IF ELSE:
- import java.util.Scanner;
- public class VowelsSum {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String word = scanner.nextLine();
- int sum = 0;
- for (int i = 0; i < word.length(); i++) {
- if (word.charAt(i) == 'a') {
- sum += 1;
- } else if (word.charAt(i) == 'e') {
- sum += 2;
- } else if (word.charAt(i) == 'i') {
- sum += 3;
- } else if (word.charAt(i) == 'o') {
- sum += 4;
- } else if (word.charAt(i) == 'u') {
- sum += 5;
- }
- }
- System.out.println(sum);
- }
- }
- ИЛИ:
- import java.util.Scanner;
- public class VowelsSum {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String word = scanner.nextLine();
- int sum = 0;
- for (char c :word.toCharArray()) {
- if (c == 'a') {
- sum += 1;
- } else if (c == 'e') {
- sum += 2;
- } else if (c == 'i') {
- sum += 3;
- } else if (c == 'o') {
- sum += 4;
- } else if (c == 'u') {
- sum += 5;
- }
- }
- System.out.println(sum);
- }
- }
- РЕШЕНИЕ СЪС SWITCH:
- import java.util.Scanner;
- public class VowelsSum {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String word = scanner.nextLine();
- int sum = 0;
- for (int i = 0; i < word.length(); i++) {
- sum += switch (word.charAt(i)) {
- case 'a' -> 1;
- case 'e' -> 2;
- case 'i' -> 3;
- case 'o' -> 4;
- case 'u' -> 5;
- default -> 0;
- };
- }
- System.out.println(sum);
- }
- }
- РЕШЕНИЕ С ТЕРНАРЕН ОПЕРАТОР:
- import java.util.Scanner;
- public class VowelsSum {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String word = scanner.nextLine();
- int sum = 0;
- for (char c : word.toCharArray()) {
- sum +=
- c == 'a' ? 1 :
- c == 'e' ? 2 :
- c == 'i' ? 3 :
- c == 'o' ? 4 :
- c == 'u' ? 5 : 0;
- }
- System.out.println(sum);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement