Advertisement
SotirovG

SumOfTwoNumbers_04

Oct 18th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.20 KB | None | 0 0
  1. package NestedLoopLab;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class SumOfTwoNumbers_04 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         int startingN = Integer.parseInt(scanner.nextLine());
  9.         int endingN = Integer.parseInt(scanner.nextLine());
  10.         int magicN = Integer.parseInt(scanner.nextLine());
  11.  
  12.         // no combination == special message !!
  13.         int combinations = 0;
  14.         boolean flag = false;
  15.         total: // тук е използван "label" под формата/прекръстен на total за да прекратя целият лууп
  16.         for (int n1 = startingN; n1 <= endingN; n1++) {
  17.             for (int n2 = startingN; n2 <= endingN; n2++) {
  18.                 combinations++;
  19.                 if (n1 + n2 == magicN) {
  20.                     flag = true;
  21.                     int result = n1 + n2;
  22.                     System.out.printf("Combination N:%d (%d + %d = %d)%n", combinations, n1, n2, result);
  23.                     break total;
  24.  
  25.                 }
  26.  
  27.             }
  28.         }if (!flag){
  29.             System.out.printf("%d combinations - neither equals %d", combinations, magicN);
  30.         }
  31.  
  32.  
  33.     }
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement