Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Empfohlene Antworten

Veröffentlicht

Meine Frage:

Kann ich irgend aus einer For-Schleife raus Variablen verändern die in der "Main" deglariert sind?

Hab mal mein Programmstück angehängt.

Geht um einen Primzahl-Tester.

Die zwei Variablen die ich abändern will oder hinein schreiben sind Grün!

Gruß & Danke

:uli

-------------------

public static void main(String[] args) {

int zahl = readInteger("Geben Sie die zu testende Zahl ein: ");

int[] teiler = new int[zahl];

int anzahlTeiler = 0;

..

..

..

for (int i = 2; i < zahl; i++) {

/** Falls die Zahl ein Teiler ist erhöhen Sie die Anzahl der Teiler und fügen Sie sie dem entsprechenden Feld hinzu. */

if ((zahl % i) != 0) {

System.out.println("Die Zahl ist nicht durch " + i + " teilbar!");

}

else {

System.out.println("Die Zahl ist durch " + i + " teilbar!");

teiler [] = i;

anzahlTeiler = anzahlTeiler + 1;

}

läufst sie doch oder, versteh ich da grad was falsch??


import static Prog1Tools.IOTools.*;  


public class PrimzahlTesterErweitert {


  public static void main(String[] args) {

    int zahl = readInteger("Geben Sie die zu testende Zahl ein: ");

    int[] teiler = new int[zahl];

    int anzahlTeiler = 0;


    if (zahl < 0) {

      System.out.println("Die Zahl muss grösser 0 sein!!!");

      System.exit(1);

    }


[COLOR="Red"]    for (int i = 2; i < zahl; i++) {

      /** Falls die Zahl ein Teiler ist erhöhen Sie die Anzahl der Teiler und fügen Sie sie dem entsprechenden Feld hinzu. */

			if ((zahl % i) != 0) { 

 			 	System.out.println("Die Zahl ist nicht durch " + i + " teilbar!"); 

			} 

			else { 

  				System.out.println("Die Zahl ist durch " + i + " teilbar!");

				teiler [] = i;

				anzahlTeiler = anzahlTeiler + 1;

				} [/COLOR]


    }


    System.out.print("Die Zahl " + zahl + " ist ");

    if (anzahlTeiler == 0) {

      System.out.println("eine Primzahl!");

    } else {

      System.out.println("keine Primzahl!");

      System.out.print("Die folgenden Zahlen sind Teiler: ");

      /** Alle Teiler ausgeben */

        if (i != anzahlTeiler - 1) {

          System.out.print(", ");

        }

      }

    }

  }

Hier das gesamte Programm ich finde die forschleife ist innerhalb der main, bekomme aber immer den Fehler dass die Variablen net gesetzt sind.


import static Prog1Tools.IOTools.*;  


public class PrimzahlTesterErweitert {


  public static void main(String[] args) {

    int zahl = readInteger("Geben Sie die zu testende Zahl ein: ");

    int[] teiler = new int[zahl];

    int anzahlTeiler = 0;


    if (zahl < 0) {

      System.out.println("Die Zahl muss grösser 0 sein!!!");

      System.exit(1);

    }


[COLOR="Red"]    for (int i = 2; i < zahl; i++) {

      /** Falls die Zahl ein Teiler ist erhöhen Sie die Anzahl der Teiler und fügen Sie sie dem entsprechenden Feld hinzu. */

			if ((zahl % i) != 0) { 

 			 	System.out.println("Die Zahl ist nicht durch " + i + " teilbar!"); 

			} 

			else { 

  				System.out.println("Die Zahl ist durch " + i + " teilbar!");

				[B]teiler [] = i;

[/B]				anzahlTeiler = anzahlTeiler + 1;

				} [/COLOR]


    }


    System.out.print("Die Zahl " + zahl + " ist ");

    if (anzahlTeiler == 0) {

      System.out.println("eine Primzahl!");

    } else {

      System.out.println("keine Primzahl!");

      System.out.print("Die folgenden Zahlen sind Teiler: ");

      /** Alle Teiler ausgeben */

        if (i != anzahlTeiler - 1) {

          System.out.print(", ");

        }

      }

    }

  }

Hier das gesamte Programm ich finde die forschleife ist innerhalb der main, bekomme aber immer den Fehler dass die Variablen net gesetzt sind.

Fällt Dir an der fett markierten Zeile etwas auf? Falls nicht, rate ich Dir dringendst die Grundlagen von Java nochmal durchzuarbeiten.

Ah danke habs gefunden, ab und zu hat man eben tomaten auf den augen:floet:

Ah danke habs gefunden, ab und zu hat man eben tomaten auf den augen:floet:

Kenn' ich nur zu gut ;)

Erstelle ein Konto oder melde dich an, um einen Kommentar zu schreiben.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.