package Uebungen.Uebung5;
import java.util.Scanner;
public class Aufgabe1 {

    public static void main(String[] args){
        // initiate scanner for input
        Scanner scanner = new Scanner(System.in);
        // primes till 100 for testing
        //int[] primes = new int[] { 2, 3,7, 13, 17, 23, 31, 37, 43, 53, 59, 67, 73, 79, 89 };
        // select number range
        while(true){
            userInterface();
            System.out.print("\n\n Check anoter range? [y/N]: ");
            if(scanner.nextLine().charAt(0)!='y') break;
            System.out.println();
        }
    }
    public static void userInterface(){
        Scanner scanner = new Scanner(System.in);
        int rangeMin=0, rangeMax=0;
        do{
            System.out.print("\nEnter lowest number from range: ");
            rangeMin = Integer.parseInt(scanner.nextLine());
            if(rangeMin<2) System.out.print("[WARNING] Number must be greater than \"1\"!\n");
        }while(rangeMin<2);

        do{
            System.out.print("\nEnter greatest number from range: ");
            rangeMax = Integer.parseInt(scanner.nextLine());
            if(rangeMax<rangeMin) System.out.print("[WARNING] Number must be at least equal or greater than \"" + rangeMin + "\"!\n");
        }while(rangeMax<rangeMin);
        int numAmount = rangeMax - rangeMin;
        int[] numbers = new int[numAmount+1];
        System.out.print("Adding numbers => ");
        for(int i=0; i<=numAmount;i++){
            numbers[i] = i+rangeMin;
            //System.out.print("number["+i+"]="+(i+rangeMin)+" ");
        }
        System.out.println();
        System.out.print("Prime numbers => ");
        for(int number:numbers){
            if(isPrime(number)) System.out.print(number + ", ");
        }
    }
    public static boolean isPrime(int number){
        boolean res=true; // output variable
        // test through all divisors form 2 to number-1
        for(int i=2;i<number;i++){
            if(number%i==0){
                res=false;
                //System.out.print("("+ number%i + ")" ); // Debug
                break;
            }
        }
        return res;
    }
}
