Question: Integer arrays originalArray and filterArray are read from input, each containing four elements. If an element in originalArray is negative, then replace the element in

Integer arrays originalArray and filterArray are read from input, each containing four elements. If an element in originalArray is negative, then replace the element in originalArray with the corresponding element in filterArray (which has only positive integers).
import java.util.Scanner;
public class ReplaceNegatives {
public static void main(String[] args){
Scanner scnr = new Scanner(System.in);
final int NUM_VALS =4;
int[] originalArray = new int[NUM_VALS];
int[] filterArray = new int[NUM_VALS];
int i;
for (i =0; i < NUM_VALS; ++i){
originalArray[i]= scnr.nextInt();
}
for (i =0; i < NUM_VALS; ++i){
filterArray[i]= scnr.nextInt();
}
for (int i =0; i <2; i++){
if (filterArray[i]==1){
originalArray[i]=0;
} else {
originalArray[i]*=5;
}
}
for (i =0; i < originalArray.length; ++i){
System.out.print(originalArray[i]+"");
}
System.out.println();
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!