Advertisement
elena_tasheva

Untitled

Feb 20th, 2020
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. import java.lang.reflect.Array;
  2. import java.util.Scanner;
  3.  
  4. public class NumberArray {
  5. public static void main(String[] args) {
  6. Scanner scanner = new Scanner(System.in);
  7.  
  8. String numbers = scanner.nextLine();
  9. String [] num = numbers.split(" ");
  10. int [] numberToManupulate = new int [num.length];
  11.  
  12. for (int i = 0; i < num.length ; i++) {
  13. numberToManupulate[i] = Integer.parseInt(num[i]);
  14. }
  15.  
  16. String command = scanner.nextLine();
  17. while(!command.equals("End")){
  18. String [] action = command.split(" ");
  19. int lenght = num.length;
  20. switch (action[0]){
  21.  
  22. case "Switch":
  23. int index = Integer. parseInt(action[1]);
  24. if(index<lenght && index>=0){
  25. if(numberToManupulate[index]!=0) {
  26. numberToManupulate[index] *= -1;
  27. }
  28. }
  29.  
  30. break;
  31. case "Change":
  32. index = Integer.parseInt(action[1]);
  33. if(index < lenght && index >= 0){
  34. numberToManupulate[index] = Integer.parseInt(action[2]);}
  35. break;
  36. case "Sum":
  37. int sumNeg = 0;
  38. int sumPos = 0;
  39. int sum = 0;
  40.  
  41. for (int i = 0; i < numberToManupulate.length; i++) {
  42. if (numberToManupulate[i] < 0) {
  43. sumNeg += numberToManupulate[i];
  44. } else {
  45. sumPos += numberToManupulate[i];
  46. }
  47. sum+=numberToManupulate[i];
  48. }
  49. if(action[1].equals("Negative")) {
  50. System.out.println(sumNeg);
  51. }
  52. if(action[1].equals("Possitive")){
  53. System.out.println(sumPos);
  54. }
  55. else if(action[1].equals("All")){
  56. System.out.println(sum);
  57. }
  58. break;
  59. }
  60.  
  61.  
  62. command = scanner.nextLine();
  63. }
  64.  
  65. for (int value : numberToManupulate) {
  66. if (value >= 0) {
  67. String numberToPrint = "" + value;
  68. System.out.print(numberToPrint + " ");
  69.  
  70. }
  71.  
  72. }
  73. }
  74.  
  75.  
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement