Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Euklid
- {
- public static void main( String[] args )
- {
- //Bei Programmaufruf müssen genau zwei Argumente übergeben worden sein sonst Fehlermeldung
- if( args != null && args.length == 2 && args[0] != null && args[1] != null)
- {
- //Argumente als int-Werte initialisieren
- //Der Aufruf von parseInt( String s ) kann NumberFormatException verursachen bei sinnlosem String-Argument
- int a = Integer.parseInt( args[0] );
- int b = Integer.parseInt( args[1] );
- //Prüfen, ob Zahlen für die Suche eines ggT(a,b) sinnvoll sind sonst Fehlermeldung
- if( a < 1 || b < 1 )
- {
- throw new IllegalArgumentException( "Falsche Eingabeparameter! Es werden zwei ganze, positive Zahlen als Argumente erwartet!" );
- }
- else
- {
- System.out.println("Der groesste gemeinsame Teiler von "+a+" und "+b+" ist "+calculateGcd( a , b ));
- }
- }
- else
- {
- throw new IllegalArgumentException( "Falsche Eingabeparameter! Es werden zwei ganze, positive Zahlen als Argumente erwartet!" );
- }
- }
- private static int calculateGcd( int v1, int v2 )
- {
- if (v1 != 0 & v2 != 0)
- {
- if ( v1 > v2 )
- {
- return calculateGcd( v1 % v2, v2);
- }
- else
- {
- return calculateGcd( v1, v2 % v1);
- }
- }
- return v1 + v2;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement