Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NestedLoopLab;
- import java.util.Scanner;
- public class SumOfTwoNumbers_04 {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int startingN = Integer.parseInt(scanner.nextLine());
- int endingN = Integer.parseInt(scanner.nextLine());
- int magicN = Integer.parseInt(scanner.nextLine());
- // no combination == special message !!
- int combinations = 0;
- boolean flag = false;
- total: // тук е използван "label" под формата/прекръстен на total за да прекратя целият лууп
- for (int n1 = startingN; n1 <= endingN; n1++) {
- for (int n2 = startingN; n2 <= endingN; n2++) {
- combinations++;
- if (n1 + n2 == magicN) {
- flag = true;
- int result = n1 + n2;
- System.out.printf("Combination N:%d (%d + %d = %d)%n", combinations, n1, n2, result);
- break total;
- }
- }
- }if (!flag){
- System.out.printf("%d combinations - neither equals %d", combinations, magicN);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement