Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Array;
- import java.util.Scanner;
- public class NumberArray {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String numbers = scanner.nextLine();
- String [] num = numbers.split(" ");
- int [] numberToManupulate = new int [num.length];
- for (int i = 0; i < num.length ; i++) {
- numberToManupulate[i] = Integer.parseInt(num[i]);
- }
- String command = scanner.nextLine();
- while(!command.equals("End")){
- String [] action = command.split(" ");
- int lenght = num.length;
- switch (action[0]){
- case "Switch":
- int index = Integer. parseInt(action[1]);
- if(index<lenght && index>=0){
- if(numberToManupulate[index]!=0) {
- numberToManupulate[index] *= -1;
- }
- }
- break;
- case "Change":
- index = Integer.parseInt(action[1]);
- if(index < lenght && index >= 0){
- numberToManupulate[index] = Integer.parseInt(action[2]);}
- break;
- case "Sum":
- int sumNeg = 0;
- int sumPos = 0;
- int sum = 0;
- for (int i = 0; i < numberToManupulate.length; i++) {
- if (numberToManupulate[i] < 0) {
- sumNeg += numberToManupulate[i];
- } else {
- sumPos += numberToManupulate[i];
- }
- sum+=numberToManupulate[i];
- }
- if(action[1].equals("Negative")) {
- System.out.println(sumNeg);
- }
- if(action[1].equals("Possitive")){
- System.out.println(sumPos);
- }
- else if(action[1].equals("All")){
- System.out.println(sum);
- }
- break;
- }
- command = scanner.nextLine();
- }
- for (int value : numberToManupulate) {
- if (value >= 0) {
- String numberToPrint = "" + value;
- System.out.print(numberToPrint + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement