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.

Hilfe benötigt

Empfohlene Antworten

Veröffentlicht

Schreiben Sie ein Programm, das alle Zahlen eines gegebenen Intervalls ausgibt, welche eine gewünschte Quersumme aufweisen. Die Anzahl dieser Zahlen soll ebenfalls ausgegeben werden.

Für das Intervall [1000, 2000] sollte bei gewünschter Quersumme = 25 die Ausgabe so aussehen:

1699

1789

1798

1879

1888

1897

1969

1978

1987

1996

Anzahl: 10

Hier meine Lösung die aber leider noch nicht ganz funktioniert und ich den Fehler einfach nicht finden kann:

n = Obergrenze

m= Untergrenze

y = gewünschte Quersumme

class Quersumme

{

public static void main(String... args)

{

int n = Integer.parseInt (args[0]);

int m = Integer.parseInt (args[1]);

int y = Integer.parseInt (args[2]);

int sum = 0;

int anz = 0;

while (n <= m)

{

sum = 0;

int x = n;

while ((x/10) != 0)

{

sum = sum+(x%10);

x = x/10;

}

sum = sum + (x%10);

if (sum == y)

{

System.out.println (n);

anz += 1;

}

n = n+1;

}

System.out.println ("Anzahl: "+anz);

}

Brauche Hilfe

Ich weiß jetzt nicht, was dein Problem ist.

Wenn ich mir deinen Quelltext kopiere klappt es ohne Probleme:


class Quersumme

{


	public static void main( String[] args ) {


		int n = 1000;

		int m = 2000;

		int y = 25;


		int sum = 0;

		int anz = 0;


		while ( n <= m ) {


			sum = 0;

			int x = n;


			while ( ( x / 10 ) != 0 ) {

				sum = sum + ( x % 10 );

				x = x / 10;

			}


			sum = sum + ( x % 10 );


			if ( sum == y ) {

				System.out.println( n );

				anz += 1;

			}


			n = n + 1;

		}

		System.out.println( "Anzahl: " + anz );


	}

}

Was ich geänder habe ist die zeile:

public static void main( String[] args ) {

Bei dir stand da

public static void main( String... args ) {

Dann gehts ohne Probleme.

Also wo genau ist dein Problem?

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

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.