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

Morgen Leute!

schaut euch bitte folgenden Code an:

import javax.swing.*;

public class TestProgramm {

public static void main (String[]args)

{

	int a=0;

	int b=0;

	int c=0;

	int d=0;

	JOptionPane.showMessageDialog(null,"Denken Sie sich eine Zahl zwischen 0 und 15!");

if (JOptionPane.showConfirmDialog(null,

        "Ist die Zahl unter diesen:   9   3   11   13   7   1   15   5    ?", "Auswahl der Zahl "  

        , JOptionPane.YES_NO_OPTION)

     == JOptionPane.YES_OPTION);

 if (false)

{

	a=0;

}

if (true)

{

	a=1;

}

 if (JOptionPane.showConfirmDialog(null,

        "Ist die Zahl unter diesen:   2   10   6   3   15   11   7   14   ?", "Auswahl der Zahl "  

        , JOptionPane.YES_NO_OPTION)

     == JOptionPane.YES_OPTION);

if (false)

{

	b=0;

}

if (true)

{

	b=1;

}

if (JOptionPane.showConfirmDialog(null,

        "Ist die Zahl unter diesen:   12   14   4   7   15   6   13   5   ?", "Auswahl der Zahl "  

        , JOptionPane.YES_NO_OPTION)

     == JOptionPane.YES_OPTION);

if (false)

{

	c=0;

}

if (true)

{

	c=1;

}

if (JOptionPane.showConfirmDialog(null,

        "Ist die Zahl unter diesen:   14   15   12   11   8   10   13   9 ?", "Auswahl der Zahl "  

        , JOptionPane.YES_NO_OPTION)

     == JOptionPane.YES_OPTION);

if (false)

{

	d=0;

}

if (true)

{

	d=1;

}


if (d==0 && c==0 && b==0 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 0");

}

if (d==0 && c==0 && b==0 && a==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 1");

}

if (d==0 && c==0 && b==1 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 2");

}

if (d==0 && c==0 && b==1 && a==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 3");

}

if (d==0 && c==1 && b==0 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 4");

}

if (d==0 && c==1 && b==0 && a==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 5");

}

if (d==0 && c==1 && b==1 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 6");

}

if (d==0 && c==1 && b==1 && a==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 7");

}

if (d==1 && c==0 && b==0 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 8");

}

if (d==1 && c==0 && b==0 && a==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist 9");

}

if (d==1 && c==0 && b==1 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist  10");

}

if (d==1 && c==0 && b==1 && a==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist  11");

}

if (d==1 && c==1 && b==0 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist  12");

}

if (d==1 && c==1 && b==0 && a==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist  13");

}

if (d==1 && c==1 && b==1 && a==0)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist  14");

}

if (d==1 && c==1 && b==1 && c==1)

{

	JOptionPane.showMessageDialog(null,"Ihre Zahl ist  15");

}

System.out.println(a+" "+b+" "+c+" "+d+" ");

}

}

Ich möchte einen Zahlentrick programmieren. Am besten schaut ihr auf diese Seite:Zahlen erraten. Das funktioniert über Binärcode. Leider spuckt das Programm immer nur 15 aus, warum ist das so?

Vielen Dank im voraus!

MFG

Ahlu-Sunnah

Hi,

du musst die Rückgabe von showConfirmDialog in eine Variable speichern und weiterverarbeiten und nicht folgendes schreiben:


		if (false) {

			b = 0;

		}

		if (true) {

			b = 1;

		}

true ist immer true also b immer 1;

Gruß Jaraz

Hi,

du musst die Rückgabe von showConfirmDialog in eine Variable speichern und weiterverarbeiten und nicht folgendes schreiben:


		if (false) {

			b = 0;

		}

		if (true) {

			b = 1;

		}

true ist immer true also b immer 1;

Gruß Jaraz

Hi Jaraz,

Ich hab dieses Programm -mit den begrenzten Wissen das ich habe- selbst geschrieben. Leider bin ich nicht so fit in Sachen programmieren. Was meinst du mit die Rückgab in einer Variablen speichern?

MFG

Ahlu-Sunnah

Probier das mal so:


if (JOptionPane.showConfirmDialog(null,

        "Ist die Zahl unter diesen:   9   3   11   13   7   1   15   5    ?", "Auswahl der Zahl "  

        , JOptionPane.YES_NO_OPTION)

     == JOptionPane.YES_OPTION)

{

	a=0;

}

else {

a=1;

}

Das was du da machst, ergibt überhaupt gar keinen Sinn. In der If-Abfrage in der du den Confirm-Dialog abfragst, machst du gar nix.

P.S.: Keine Garantie für eventuelle Syntax- oder Tippfehler

Also den unkompliziertesten Weg hat Grueni mit der korrigierten if Variante erklärt.

nochmals zur Erläuterung für die if Abfrage:



if ( wenn Anweisung korrekt- also zahl in Reihe vorhanden )

{

   tue irgendwas ( variable z.b. a = 1)

}

else

{

  tue irgendwas ( variable z.b. a=0 )

}


@ahlu-sunnah

Anmerkung: Man sollte nicht einfach mit Codeschnipseln losprogrammieren, solange man nicht die super BASICS des Programmierens kennt. Solche Fragen wie was ist eine Variable oder wie geht eine if Abfrage müssten da im Vorfeld schon geklärt sein. Ich empfehle hierbei ein Buch zur Hadn zu nehmen, z.B. Java als erste Programmiersprache (weis den verlag gerade nicht)

Eine fortgeschrittene Lösung wäre es, wenn du eine Bitmaske erstellst und die gegebene 0 oder 1 immer um ein Bit nach links "Shiftest" um dann die komplette 4 Bit lange Zahl auszulesen, dann würden die ganzen if Abfragen für die Auswertung wegfallen.

Have fun

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.