Zahl mit maximaler Anzahl von Teiler

serious

Mitglied
Hallo liebe Community,
ich soll ein Applet erstellen, welches im Intervall von 1 bis zu einer Zahl i (i aus N, wird vom Benutzer eingegeben) die Zahl mit den meisten Teilern und die Anzahl der Teiler ausgibt.
Komischer Weise gibt mein Applet immer die Zahl i als Zahl mit maximaler Anzahl von Divisoren an, die Anzahl der Teiler stimmt aber.

Java:
 public class DivisorApplet extends Applet {
        Label inputPrompt;        // declare Label
        TextField input, output;  // declare textfields for input and output
    
        int i,			  	// the input integer read from terminal = upper number limit
        	n,            	// number between 1 and i
        	divisor,	  	// tested divisor of n
        	divisorNumber, 	// number of divisors of n
            maxDivisors, 	// max number of possible divisors
            maxnumber;  	// number with max divisors between 1 and i
        
    //{... Layoutkram...}
      
        public void findMaximumDivisors() {   
                // get input number
                i = Integer.parseInt(input.getText());
           
                for (n = 2; n <= i; n++ ) { //increases n until n=i
                	divisorNumber = 0; 
                	maxDivisors = 1;		// 1 is divisor of every number
                	maxnumber = 1;
                	
                	for ( divisor = 1; divisor <= n; divisor++ ) { //increases divisor until divisor=n
                	
                			if ( n % divisor == 0 )		//count divisors of n
                				divisorNumber++;
                	}
                	if ( divisorNumber > maxDivisors) {	//compare current divisors of n with maxDivisor
                		maxDivisors = divisorNumber;
                		maxnumber = n;					//save n as number with max divisor
                	}
                }
           
                // define the output string
                String str = "Die Zahl "  
                        + maxnumber + " hat " + maxDivisors + ". Das ist das Maximum unter 1 bis " + i + '.';

                // put the sting into the output field      
                output.setText( str );  
        }

Ich finden den Fehler der Methode einfach nicht. Könnt ihr mir helfen?

Mit freundlichen Grüßen,
Serious
 
Du musst
[JAVA=20]
maxDivisors = 1; // 1 is divisor of every number
maxnumber = 1;
[/code]
aus der schleife rausnehmen. z.B. zu der Zeile 16. Sonst ist es bei jedem Durchlauf wieder die 1 und da sich jede andere Zahl durch 1 und sich selber teilen lässt, überschreibst du es jedes mal, bis du dann bei
Code:
i
bist.
 

Neue Themen


Zurück
Oben