Advertisement
Lyuben_Andreev

Untitled

May 31st, 2020
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.58 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class Main {
  4.  
  5.     public static void main(String[] args) {
  6. //        double[] money =
  7. //                {       10, 24, 12, 12.50, 33.33,
  8. //                        112.51, 102.50, 1250, 1.1, 0.33,
  9. //                        0.44, 1.12
  10. //                };
  11.  
  12.  
  13.         Scanner sc = new Scanner(System.in);
  14.         int size = Integer.parseInt(sc.nextLine());
  15.         double[] money = new double[size];
  16.  
  17.         for (int i = 0; i < money.length; i++) {
  18.             money[i] = Double.parseDouble(sc.nextLine());
  19.         }
  20.  
  21.         int startPosition = Integer.parseInt(sc.nextLine());
  22.         double sum = money[startPosition];
  23.  
  24.         String direction = sc.nextLine();
  25.  
  26.         while (!direction.equals("END")) {
  27.             if (direction.equals("L")) {
  28.                 if (startPosition == 0) {
  29.                     System.out.println("Cannot move left. Please move right.");
  30.                 } else {
  31.                     startPosition = startPosition - 1;
  32.                     sum += money[startPosition];
  33.                 }
  34.             } else if (direction.equals("R")) {
  35.                 if (startPosition == money.length - 1) {
  36.                     System.out.println("Cannot move right. Please move left.");
  37.                 } else {
  38.                     startPosition = startPosition + 1;
  39.                     sum += money[startPosition];
  40.                 }
  41.             } else {
  42.                 System.out.println("Invalid direction");
  43.             }
  44.             direction = sc.nextLine();
  45.         }
  46.  
  47.         System.out.println(sum);
  48.     }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement