SHARE
TWEET

Untitled

a guest Dec 6th, 2019 85 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class HomeworkMove {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         int start = Integer.parseInt(scanner.nextLine());
  9.         String[] array = scanner.nextLine().split("\\s*,\\s*");
  10.         int[] values = new int[array.length];
  11.         for (int i = 0; i < values.length; i++) {
  12.             values[i] = Integer.parseInt(array[i]);
  13.         }
  14.         int forward = 0;
  15.         int backwards = 0;
  16.         String command = scanner.nextLine();
  17.         while (!command.equals("exit")){
  18.  
  19.             int steps = Integer.parseInt(command.substring(0, 1));
  20.             String direction = command.substring(2, command.length()-2);
  21.             int size = Integer.parseInt(command.substring(command.length()-1));
  22.  
  23.  
  24.             if (direction.equals("forward")){
  25.                 for (int i = 0; i < steps; i++) {
  26.                     int startCounting = (start + size) % values.length;
  27.                     forward += values[startCounting];
  28.                     start = startCounting;
  29.                 }
  30.             }
  31.             else if (direction.equals("backwards")){
  32.                 for (int i = 0; i < steps; i++) {
  33.                     int startCounting = start - size;
  34.                     if (startCounting < 0){
  35.                         startCounting += values.length;
  36.                     }
  37.                     backwards += values[startCounting];
  38.                     start = startCounting;
  39.                 }
  40.             }
  41.             command = scanner.nextLine();
  42.         }
  43.         System.out.printf("Forward: %d%n", forward);
  44.         System.out.printf("Backwards: %d", backwards);
  45.     }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top