Advertisement
Spocoman

Name Game

Sep 8th, 2024
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.68 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.         String name, winner = "";
  7.         int num, points = 0;
  8.  
  9.         while (!(name = scanner.nextLine()).equals("Stop")) {
  10.             int currentPoints = 0;
  11.             for (int i = 0; i < name.length(); i++) {
  12.                 num = Integer.parseInt(scanner.nextLine());
  13.                 int z = name.charAt(i);
  14.  
  15.                 if (num == z) {
  16.                     currentPoints += 10;
  17.                 } else {
  18.                     currentPoints += 2;
  19.                 }
  20.             }
  21.  
  22.             if (currentPoints >= points) {
  23.                 winner = name;
  24.                 points = currentPoints;
  25.             }
  26.         }
  27.  
  28.         System.out.println("The winner is " + winner + " with " + points + " points!");
  29.     }
  30. }
  31.  
  32. Решение с тернарен оператор и леко тарикатската:
  33.  
  34. import java.util.Scanner;
  35.  
  36. public class Main {
  37.     public static void main(String[] args) {
  38.         Scanner scanner = new Scanner(System.in);
  39.         String name, winner = "";
  40.         int points = 0;
  41.  
  42.         while (!(name = scanner.nextLine()).equals("Stop")) {
  43.             int currentPoints = 0;
  44.             for (int i = 0; i < name.length(); i++) {
  45.                 currentPoints += Integer.parseInt(scanner.nextLine()) == name.charAt(i) ? 10 : 2;
  46.                 if (currentPoints >= points) {
  47.                     winner = name;
  48.                     points = currentPoints;
  49.                 }
  50.             }
  51.         }
  52.        
  53.         System.out.printf("The winner is %s with %d points!", winner, points);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement