SHARE
TWEET

Untitled

Eugene0091 Oct 21st, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.company;
  2.  
  3. import java.io.*;
  4. import java.util.Scanner;
  5.  
  6. public class Main {
  7.     public static int IsCorrectInput(int Number, int Min, int Max) {
  8.         boolean IsCorrect = true;
  9.         Scanner scan = new Scanner(System.in);
  10.         do {
  11.             try {
  12.                 Number = scan.nextInt();
  13.                 if ((Number > Min - 1) && (Number < Max + 1)) {
  14.                     IsCorrect = false;
  15.                 } else {
  16.                     System.out.println("Error! Enter a number. The number must be between " + Min + " and " + Max + "!");
  17.                 }
  18.             } catch (Exception e){
  19.                 System.out.println("Error! Enter a number: ");
  20.                 scan.next();
  21.             }
  22.         } while(IsCorrect);
  23.         IsCorrect = true;
  24.         return Number;
  25.     }
  26.  
  27.     public static void OutputConsole(int[] Number) {
  28.         int i = 0;
  29.         while(i < Number.length) {
  30.             System.out.println(Number[i] + " | " + Number[i + 1]);
  31.             i = i + 2;
  32.         }
  33.     }
  34.  
  35.         public static void FindFriendshipNumbers(int Range, int[] Number){
  36.             int i, j, k, p;
  37.             int[] del = new int[Range];
  38.             for (k = 2; k < Range; k++) {
  39.                 del[k] = k + 1;
  40.                 for (k = 2; k < Range / 2 + 1; k++){
  41.                     p = k + k;
  42.                     while (p < Range + 1){
  43.                         del[p] = del[p] + k;
  44.                         p = p + k;
  45.                     }
  46.                 }
  47.             }
  48.             for (i = 2; i < Range ; i++) {
  49.                 for (j = i + 1; j < Range + 1; j++){
  50.                     if ((del[i] == i + j) & (del[j] == i + j)){
  51.  
  52.                         Number[Number.length - 1] = i;
  53.                         Number[Number.length] = j;
  54.                     }
  55.                 }
  56.             }
  57.         }
  58.  
  59.     public static void OutputFile(int[] Number) throws IOException {
  60.         int i = 0;
  61.         BufferedWriter writer = new BufferedWriter(new FileWriter("Output"));
  62.         while (i < Number.length) {
  63.             writer.write(Number[i] + " | " + Number[i + 1]);
  64.             i = i + 2;
  65.         }
  66.         writer.close();
  67.     }
  68.  
  69.     public static void InputFile(int Range) {
  70.         File UserFile new File("Input.txt");
  71.         PrintWriter pw = new PrintWriter(UserFile);
  72.         pw.println(Range);
  73.         pw.close();
  74.     }
  75.  
  76.     public static char GetUserChoice() {
  77.         char Choice;
  78.         System.out.println("Do you want to read fromm file?[Y/N]");
  79.         Scanner scan = new Scanner(System.in);
  80.         Choice = scan.nextInt();
  81.         return Choice;
  82.     }
  83.  
  84.     public static void InputFile() {
  85.         try {
  86.             File file = new File("input.txt");
  87.             if (!file.exists())
  88.                 file.createNewFile();
  89.             PrintWriter pw = new PrintWriter(file);
  90.             pw.println("Hello, World!");
  91.             pw.close();
  92.         } catch(IOException e) {
  93.             System.out.print("Error");
  94.         }
  95.  
  96.     }
  97.  
  98.     public static void main(String[] args) {
  99.         char Answer;
  100.         int Range;
  101.         int[] Number;
  102.         System.out.println("This program finds pairs of friendly numbers.");
  103.         Answer = GetUserChoice();
  104.         if (Answer = "Y") {
  105.             InputFile(Range);
  106.             System.out.println(Range);
  107.             else
  108.                 System.out.println("Enter the upper limit of the friendly number search range.");
  109.             System.out.println("The number must be up to 10.000 !");
  110.             IsCorrectInput(Range + 2 + 10000);
  111.         }
  112.         FindFriendshipNumbers(Range + Number);
  113.         OutputConsole(Number);
  114.         OutputFile(Number);
  115.     }
  116. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top