Advertisement
uktcar

07.2.06NameGame

Mar 22nd, 2023
549
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.02 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class exam06NameGame {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String playerName = scanner.nextLine();
  8.  
  9.         int bestScores = 0;
  10.         String bestPlayer = "";
  11.  
  12.         while (!playerName.equals("Stop")) {
  13.             int nameLength = playerName.length();
  14.             int scores = 0;
  15.  
  16.             for (int i = 0; i < nameLength; i++) {
  17.                 char letter = playerName.charAt(i);
  18.                 int number = Integer.parseInt(scanner.nextLine());
  19.  
  20.                 if (letter == number) {
  21.                     scores += 10;
  22.                 } else {
  23.                     scores += 2;
  24.                 }
  25.             }
  26.  
  27.             if (scores >= bestScores) {
  28.                 bestScores = scores;
  29.                 bestPlayer = playerName;
  30.             }
  31.  
  32.             playerName = scanner.nextLine();
  33.         }
  34.  
  35.         System.out.printf("The winner is %s with %d points!", bestPlayer, bestScores);
  36.     }
  37. }
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement