Autor Beitrag
0jeppa
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 09:58 
Hallo Zusammen,

leider habe ich noch grosse mühe mit der Programmierung. Dieses Beispiel sollen wir lösen, aber selbst da stehe ich schon an. Eigentlich sollte diese Aufgabe ja nicht so schwierig sein, aber ich weiss schon garnicht so richtig wie anfangen.
Wäre für eure Hilfe wirklich sehr dankbar.
Dies ist die Aufgabe:

Die Integerzahl c lässt sich aus den beiden Ganzzahlen a und b bestimmen.

c = 0 für a < 3 und b < 5
c = 1 für 3 <= a < 7 und b < 5
c = 2 für 3 <= a < 7 und 5 <= b < 10
c = 3 für 7 <= a und 5 <= b < 10
c = 4 sonst

Viele Grüsse

Thomas


Zuletzt bearbeitet von 0jeppa am So 18.10.09 11:03, insgesamt 1-mal bearbeitet
JüTho
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2021
Erhaltene Danke: 6

Win XP Prof
C# 2.0 (#D für NET 2.0, dazu Firebird); früher Delphi 5 und Delphi 2005 Pro
BeitragVerfasst: So 18.10.09 10:41 
Hallo und :welcome:

Zunächst solltest du einen sinnvolleren Titel wählen: "Anfängerschwierigkeiten" ist völlig nichtssagend, siehe auch Forumshilfe. Das kannst du noch ändern: klicke den Schere-Button bei deinem Beitrag, ändere den Titel, dann "Absenden".

Zum Problem: Es handelt sich doch nur um verschachtelte Prüfungen: Wenn a diese Bedingung erfüllt und b dieser Bedingung, dann hat c den ersten Wert; andernfalls gilt: Wenn a der nächsten Bedingung entspricht usw. "Wenn" heißt auf englisch "if". Zu if gibt es else und else if; für Verschachtelungen gibt es Klammern () sowie "und" && und "oder" ||.

Mehr dazu findest du in der SDK-Doku/MSDN oder auch in OpenBook Visual C#.

Du musst also nur die gesammelten Bedingungen sorgfältig aufdröseln. Jürgen
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 11:10 
Hallo

Danke für die schnelle Antwort und die Tips.
Eigentlich sind mir die if-anweisungen soweit klar. Ich habe einfach das Problem, das ich nicht weiss wie ich das mit c# umsetzen kann, also sprich was ich eingeben muss.

So habe ich es mal eingegeben, aber das stimmt ja nicht.
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
Console.WriteLine("a = ; b = ");

            int c = 0;

            if ( a < 3 && b < 5);


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 18.10.09 11:24 
Naja, Du musst ja noch hinschreiben, was passieren soll, wenn die Bedingung wahr ist. Ein Semikolon ist da ein bisschen wenig ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 12:55 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
static void Main(string[] args)
        {
            Console.WriteLine("a = ");

            int c = 0;
      
            if ( a < 3 );

            {Console.WriteLine(c = 0);

            if (a > 3);

            Console.WriteLine (c = 1);



Wieso funktioniert das nicht?
Ich probiere jetzt schon mehrere Stunden daran rum und komme einfach auf keine Lösung.

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 18.10.09 13:00 
Du machst nach dem if ja nichts. Durch das Semikolon beendest du das if gleich wieder...
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
if (irgendwas)
  MachWas();

if (irgendwas)
{
  MachWas();
  MachWas();
}
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 13:30 
Ich komme da nicht weiter, vielleicht gibt es ja irgendjemand der mir das Programm zu dem Problem schreiben kann, damit ich überhaupt erstmal weiss was ich da alles machen muss.
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 13:42 
ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
Console.WriteLine("a = ");

            int c = 0;
            int a = 0;
            int b = 0;

            if (a < 3 && b < 5)
            {
                Console.WriteLine(c = 0);
            }
            {
                if (a > 3 && b < 5)

                    Console.WriteLine(c = 1);

                if (3 < a && a < 7 && 5 <= b && b < 10)

                    Console.WriteLine(c = 2);
                if (7 <= a && 5 <= b && b < 10)

                    Console.WriteLine(c = 3);
                else
                    Console.WriteLine(c = 4);


Habe nochmal etwas angepasst, vielleicht kann mir jemand sagen, warum es noch nicht so funktioniert wie es sollte.

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 18.10.09 13:50 
Wenn da steht <=, dann solltest du das auch schreiben (hast du bei a nicht überall wie in der Liste). Und es fehlen ein paar else, da sonst das else nur für das letzte if gilt.

Veränderst du die Werte von a und b denn? Wenn du die fest auf 0 setzt, kann es ja nicht klappen.

Und im Forum solltest du die C# Tags verwenden: <span class="inlineSyntax">{PROTECTTAGa3dd33d2b2536ff99b7270dd32390392}</span>
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 14:03 
Danke für den Tip, habe es nochmal angepasst

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
 Console.WriteLine("a = ");

            int c = 0;
            int a = 0;
            int b = 0;

            if (a < 3 && b < 5)
            {
                Console.WriteLine(c = 0);
            }
            {
                if (a > 3 && b < 5)

                    Console.WriteLine(c = 1);
                else

                if (3 <= a && a < 7 && b < 5)

                    Console.WriteLine(c = 2);
                else

                if (7 <= a && 5 <= b && b < 10)

                    Console.WriteLine(c = 3);
                else
                    Console.WriteLine(c = 4);


Wie kann ich es denn programmieren, das ich für a und b immer andere werte eingeben kann?

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 18.10.09 14:06 
user profile icon0jeppa hat folgendes geschrieben Zum zitierten Posting springen:
Wie kann ich es denn programmieren, das ich für a und b immer andere werte eingeben kann?
Hmm, du willst einen Wert eingeben lassen, nicht ausgeben. Also nicht WriteLine sondern... hmm, wie wäre es mit [url=msdn.microsoft.com/d...nsole.readline.aspx]ReadLine[/url]? :zwinker:
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 14:15 
Ok, nun komme ich der Sache immer näher, glaube ich zumindest.
Kann jetzt Werte für a und b eingeben, aber das c wird nicht ausgegeben.
Wenn ich int a=0 und int b=0 wegnehme kommt mir gleich ein Fehler. Was ist noch falsch?

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
 static void Main(string[] args)
        {
            Console.WriteLine("a = ");
            Console.ReadLine();

            Console.WriteLine("b = ");
            Console.ReadLine();


            int c = 0;
            int a = 0;
            int b = 0;

            if (a < 3 && b < 5)
            {
                Console.WriteLine(c = 0);
            }
            {
                if (a > 3 && b < 5)

                    Console.WriteLine(c = 1);
                else

                if (3 <= a && a < 7 && b < 5)

                    Console.WriteLine(c = 2);
                else

                if (7 <= a && 5 <= b && b < 10)

                    Console.WriteLine(c = 3);
                else
                    Console.WriteLine(c = 4);


Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 18.10.09 14:21 
Du ignorierst ja auch die Eingabe, die dir ReadLine zurückgibt...
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 14:35 
Ja, stimmt.
aber ich kann ja nicht sagen if (console.readline < 3) anstelle (a < 3)
Ich weiss nicht wie ich das umsetzen kann.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 18.10.09 14:39 
Du musst den eingelesenen Wert in einen Integerwert umwandeln falls möglich und damit dann arbeiten.
ausblenden C#-Quelltext
1:
2:
3:
int MyValue = 0;
if Int32.TryParse(Console.ReadLine(), out MyValue)
  Console.WriteLine(MyValue);
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 15:05 
Wo muss ich das reinbasteln, grad am Anfang?
Diese Begriffe sagen mir nichts.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: So 18.10.09 15:09 
user profile icon0jeppa hat folgendes geschrieben Zum zitierten Posting springen:
Wo muss ich das reinbasteln, grad am Anfang?

Wo willst Du die Werte den einlesen? Am Ende macht's wohl einen Sinn ;-)

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 15:19 
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            

            Console.WriteLine("a = ");
            Console.ReadLine();
            
            Console.WriteLine("b = ");
            Console.ReadLine();


            int MyValue = 0;
            if (Int32.TryParse(Console.ReadLine(), out MyValue))
                Console.WriteLine(MyValue);


       
            if (a < 3 && b < 5)
            {
                Console.WriteLine(c = 0);
                int c = Convert.ToInt32(Console.ReadLine());
            }
            {
                if (a > 3 && b < 5)

                    Console.WriteLine(c = 1);
                else

                if (3 <= a && a < 7 && b < 5)

                    Console.WriteLine(c = 2);
                else

                if (7 <= a && 5 <= b && b < 10)

                    Console.WriteLine(c = 3);
                else
                    Console.WriteLine(c = 4);



Hilfe benötigt, was muss ich tun???

Moderiert von user profile iconChristian S.: C#-Tags hinzugefügt
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19326
Erhaltene Danke: 1749

W11 x64 (Chrome, Edge)
Delphi 12 Pro, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 18.10.09 15:21 
Sorry, aber es bringt nichts, wenn du einfach etwas hineinkopierst ohne irgendwie zu versuchen zu verstehen was da passiert. :nixweiss:
0jeppa Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 24



BeitragVerfasst: So 18.10.09 15:38 
Natürlich versuche ich es zu verstehen, auch wenn es vielleicht manchmal nicht danach aussieht.

Ich muss also a und b konvertieren, ist das richtig?

int a = Convert.ToInt32(Console.ReadLine());