[Opgelost] Hit The Target - Java Task

  

2
Onderwerp starter

Write a program that takes as input an integer – the target – and outputs to the console all pairs of numbers between 1 and 20, which, if added or subtracted, result in the target:

hit the target - java task

1 antwoord
2

Very interesting task, my friend! Here is what I have as a solution:

import java.util.Scanner;
 
public class Pr_09_HitTheTarget {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int result = scanner.nextInt();
 
        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j <= 20; j++) {
                if (i + j == result) {
                    System.out.println(i + "+" + j + "=" + result);
                }
            }
        }
 
        for (int i = 1; i <= 20; i++) {
            for (int j = 1; j <= 20; j++) {
                if (i - j == result) {
                    System.out.println(i + "-" + j + "=" + result);
                }
            }
        }
    }
}
Deel: