Професионална програма
Loading...
zdravkostov avatar zdravkostov 4 Точки

11. *Array Manipulator, Java, 40/100

package JavaFundamentals;

import java.util.Scanner;

public class ArrayManipulator {
    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        String[] stringNumbers = scan.nextLine().split(" ");
        int[] intNumbers = new int[stringNumbers.length];

        for (int i = 0; i < stringNumbers.length; i++) {
            intNumbers[i] = Integer.parseInt(stringNumbers[i]);
        }

        String nextManipulation = scan.nextLine();
        int subNumber;
        int index;
        int theMostNumber;
        while (!nextManipulation.equals("end")) {
            String[] currentCommand = nextManipulation.split(" ");
            switch (currentCommand[0]) {
                case "exchange":
                    index = Integer.parseInt(currentCommand[1]);
                    if (index > intNumbers.length - 1) {
                        System.out.println("Invalid index");
                        break;
                    }
                    exchange(index, intNumbers);
                    break;

                case "max":
                    theMostNumber = Integer.MIN_VALUE;
                    switch (currentCommand[1]) {
                        case "even":
                            maxEven(intNumbers, theMostNumber);
                            break;
                        case "odd":
                            maxOdd(intNumbers, theMostNumber);
                            break;
                    }
                    break;

                case "min":
                    theMostNumber = Integer.MAX_VALUE;
                    switch (currentCommand[1]) {
                        case "even":
                            minEven(intNumbers, theMostNumber);
                            break;
                        case "odd":
                            minOdd(intNumbers, theMostNumber);
                            break;
                    }
                    break;

                case "first":
                    subNumber = Integer.parseInt(currentCommand[1]);
                    if (subNumber > intNumbers.length) {
                        System.out.println("Invalid count");
                        break;
                    }
                    switch (currentCommand[2]) {
                        case "even":
                            firstEven(intNumbers, subNumber);
                            break;
                        case "odd":
                            firstOdd(intNumbers, subNumber);
                            break;
                    }
                    break;

                case "last":
                    subNumber = Integer.parseInt(currentCommand[1]);
                    if (subNumber > intNumbers.length) {
                        System.out.println("Invalid count");
                        break;
                    }
                    switch (currentCommand[2]) {
                        case "even":
                            lastEven(intNumbers, subNumber);
                            break;
                        case "odd":
                            lastOdd(intNumbers, subNumber);
                            break;
                    }
                    break;
            }
            nextManipulation = scan.nextLine();
        }

        System.out.print("[");
        for (int i = 0; i < intNumbers.length; i++) {
            if (i != 0) {
                System.out.print(", " + intNumbers[i]);
            } else {
                System.out.print(intNumbers[i]);
            }
        }
        System.out.println("]");

    }

    public static void exchange(int index, int[] array) {
        int counter = 0;
        for (int i = 0; i < array.length; i++) {        //count the elements after given index
            if (i > index) {
                counter++;
            }
        }
        int[] secondArray = new int[counter];
        int secondIndex = 0;
        for (int i = index + 1; i < array.length; i++) {      //put them in different array
            secondArray[secondIndex] = array[i];
            secondIndex++;
        }
        secondIndex = array.length - 1;
        for (int i = index; i >= 0; i--) {      //fill their gaps with the starting array, AKA shift them all (counter) times right
            array[secondIndex] = array[i];
            secondIndex--;
        }
        for (int i = 0; i < counter; i++) {     //fill the new gaps from the starting array with the elements from the second array
            array[i] = secondArray[i];
        }
    }

    public static void maxEven(int[] array, int theBiggestEvenNumber) {
        boolean found = false;
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= theBiggestEvenNumber && array[i] % 2 == 0) {
                found = true;
                index = i;
                theBiggestEvenNumber = array[i];
            }
        }
        if (found == false) {
            System.out.println("No matches");
        } else {
            System.out.println(index);
        }
    }

    public static void maxOdd(int[] array, int theBiggestOddNumber) {
        boolean found = false;
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] >= theBiggestOddNumber && array[i] % 2 == 1) {
                found = true;
                index = i;
                theBiggestOddNumber = array[i];
            }
        }
        if (found == false) {
            System.out.println("No matches");
        } else {
            System.out.println(index);
        }
    }

    public static void minEven(int[] array, int theSmallestEvenNumber) {
        boolean found = false;
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] <= theSmallestEvenNumber && array[i] % 2 == 0) {
                found = true;
                index = i;
                theSmallestEvenNumber = array[i];
            }
        }
        if (found == false) {
            System.out.println("No matches");
        } else {
            System.out.println(index);
        }
    }

    public static void minOdd(int[] array, int theSmallestOddNumber) {
        boolean found = false;
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            if (array[i] <= theSmallestOddNumber && array[i] % 2 == 1) {
                found = true;
                index = i;
                theSmallestOddNumber = array[i];
            }
        }
        if (found == false) {
            System.out.println("No matches");
        } else {
            System.out.println(index);
        }
    }

    public static void firstEven(int[] array, int numberOfNumbers) {
        int counter = 0;
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 == 0) {
                if (counter == 0) {
                    System.out.print(array[i]);
                } else {
                    System.out.print(", " + array[i]);
                }
                counter++;
                if (counter == numberOfNumbers) {
                    break;
                }
            }
        }
        System.out.println("]");
    }

    public static void firstOdd(int[] array, int numberOfNumbers) {
        int counter = 0;
        System.out.print("[");
        for (int i = 0; i < array.length; i++) {
            if (array[i] % 2 == 1) {
                if (counter == 0) {
                    System.out.print(array[i]);
                } else {
                    System.out.print(", " + array[i]);
                }
                counter++;
                if (counter == numberOfNumbers) {
                    break;
                }
            }
        }
        System.out.println("]");
    }

    public static void lastEven(int[] array, int numberOfNumbers) {
        int counter = 0;
        System.out.print("[");
        for (int i = array.length - 1; i >= 0; i--) {
            if (array[i] % 2 == 0) {
                if (counter == 0) {
                    System.out.print(array[i]);
                } else {
                    System.out.print(", " + array[i]);
                }
                counter++;
                if (counter == numberOfNumbers) {
                    break;
                }
            }
        }
        System.out.println("]");
    }

    public static void lastOdd(int[] array, int numberOfNumbers) {
        int counter = 0;
        System.out.print("[");
        for (int i = array.length - 1; i >= 0; i--) {
            if (array[i] % 2 == 1) {
                if (counter == 0) {
                    System.out.print(array[i]);
                } else {
                    System.out.print(", " + array[i]);
                }
                counter++;
                if (counter == numberOfNumbers) {
                    break;
                }
            }
        }
        System.out.println("]");
    }

}
Тагове:
0
Java Fundamentals
MANEKENA avatar MANEKENA 118 Точки
case "exchange":
                    index = Integer.parseInt(currentCommand[1]);
                    if (index > intNumbers.length - 1) {

още на първия ти кейс имаш проблем , подаденият индекс може и да е < 0 

if (index > intNumbers.length - 1 || index <0 ) - така трябва да ти изглежда проверката

не съм гледал кода надолу защото ще ми отнеме доста време  , направо ще ти дам моя код за да си сравниш:

https://github.com/Petrov-connect/Java-Fundamentals/blob/master/Methods/ArrayManipulator.java

0
zdravkostov avatar zdravkostov 4 Точки

Благодаря!

0