Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class var_27_task_4 {
- public static void main(String[] args){
- Scanner scanner = new Scanner(System.in);
- boolean isIncorrect;
- int n = 0;
- do {
- System.out.print("Enter n (size of arrays A and B) : ");
- isIncorrect = false;
- try {
- n = Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException exception) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- if (!isIncorrect && (n <= 0)) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- } while (isIncorrect);
- int a[] = new int[n];
- Arrays.fill(a, 0);
- int b[] = new int[n];
- Arrays.fill(b, 0);
- for (int i = 0; i < n; i++) {
- do {
- System.out.print("Enter an integer A[" + (i+1) + "] ");
- isIncorrect = false;
- try {
- a[i] = Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException exception) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- } while (isIncorrect);
- }
- for (int i = 0; i < n; i++) {
- do {
- System.out.print("Enter an integer B[" + (i+1) + "] ");
- isIncorrect = false;
- try {
- b[i] = Integer.parseInt(scanner.nextLine());
- } catch (NumberFormatException exception) {
- isIncorrect = true;
- System.err.println("Input Error. Check if the data is correct.");
- }
- } while (isIncorrect);
- }
- scanner.close();
- for (int i = 0; i < n; ++i) {
- int temp = a[i];
- a[i] = b[i];
- b[i] = -temp;
- }
- System.out.print("New arrays A and B: \n");
- for (int i = 0; i < n; ++i) {
- System.out.print("A[" + (i+1) + "] = " + a[i] + ", ");
- System.out.print("B[" + (i+1) + "] = " + b[i] + "\n");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement