hallo, ich möchte ein programm schreiben um zwei zahlen einzulesen und dann den größten gemeinsamen teiler davon ausrechnen zu lassen. bin schon fast fertig nur ich habe ein problem dass das programm bei der eingabe 0 0 beendet werden soll. wär super wenn mir jemand meinen fehler aufzeigen könnte. :toll: hier der code:
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* This program is to read two numbers and calculate their greatest
* divider in common
* @throws IOException Throws an IOException
*
*/
public final class Shell {
private Shell() { }
/*
* This is the main method
*/
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader (new
InputStreamReader (System.in));
execute(in);
}
//auxiliary method to read two numbers
private static void execute(BufferedReader in) throws IOException {
boolean quit = false;
while (!quit){
boolean error = false;
System.out.println("gcd> ");
String input = in.readLine();
String[] tokens = input.trim().split("\\s+");
if (tokens.length != 2){
System.out.println("Error! 2 args!");
error = true;
}
if(!error) {
try{
int i = Integer.parseInt(tokens[0]);
int j = Integer.parseInt(tokens[1]);
if ( i == 0 && j == 0){
break;
} else {
System.out.println("gcd(" + i + ", " + j + ") = "
+ gcd(i,j));
}
} catch(NumberFormatException e) {
System.err.println("Error! Not numeric!");
error = true;
}
}}
}
// method to calculate the greatest divider in common of both numbers
public static int gcd ( int x, int y){
if (y < 0){
return gcd(x, -y);
}else if (x < 0){
return gcd(-x , y);
}else if ( y == 0){
return Math.abs(x);
}else if (x > y){
return gcd(x-y,y);
}else if (y > x){
return gcd (y , x);
}else if (x == y){
return x;
}else {
return -1;
}
}
}
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
/**
* This program is to read two numbers and calculate their greatest
* divider in common
* @throws IOException Throws an IOException
*
*/
public final class Shell {
private Shell() { }
/*
* This is the main method
*/
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader (new
InputStreamReader (System.in));
execute(in);
}
//auxiliary method to read two numbers
private static void execute(BufferedReader in) throws IOException {
boolean quit = false;
while (!quit){
boolean error = false;
System.out.println("gcd> ");
String input = in.readLine();
String[] tokens = input.trim().split("\\s+");
if (tokens.length != 2){
System.out.println("Error! 2 args!");
error = true;
}
if(!error) {
try{
int i = Integer.parseInt(tokens[0]);
int j = Integer.parseInt(tokens[1]);
if ( i == 0 && j == 0){
break;
} else {
System.out.println("gcd(" + i + ", " + j + ") = "
+ gcd(i,j));
}
} catch(NumberFormatException e) {
System.err.println("Error! Not numeric!");
error = true;
}
}}
}
// method to calculate the greatest divider in common of both numbers
public static int gcd ( int x, int y){
if (y < 0){
return gcd(x, -y);
}else if (x < 0){
return gcd(-x , y);
}else if ( y == 0){
return Math.abs(x);
}else if (x > y){
return gcd(x-y,y);
}else if (y > x){
return gcd (y , x);
}else if (x == y){
return x;
}else {
return -1;
}
}
}