[C#]1 Jahr u. 2 Monate anstatt 14 Monate

04/23/2011 16:12 .BritainAndy#1
Hi Com,

sorry erstmal für den Fenstertitel, mir ist nichts eingefallen.
Also mein Problem ist folgendes:
Ich habe ein Programmer gecodet, wo man sein Einkommen, seine Ausgaben und sein Wunsch betrag eingeben muss .
So und als ergebnis wird in ein Label dann geschrieben "Du musstr noch xx monate warten bis du dein wunschbetrag erreicht hast".
Klappt alles wunderbar, nur ich will dass anstatt "Du hast in 1 Jahr oder 14 Monaten dein Betrag" lieber "Du hast in 1,2 Jahren dein Betragt .." ausgegeben wird.
Nur weiß ich nicht, wie ich das mache. Hier mein Source:

Code:
        private void button1_Click(object sender, EventArgs e)
        {

            int kontostand = Convert.ToInt32(txtEinkommen.Text);
            int ausgaben = Convert.ToInt32(txtAusgaben.Text);
            int gewinn = (Convert.ToInt32(txtEinkommen.Text)) - (Convert.ToInt32(txtAusgaben.Text));
            int wunsch = Convert.ToInt32(textBox1.Text);
            int monate = 0;

            while (kontostand < wunsch)
            {
                monate++;
                kontostand = kontostand + gewinn;
            }

            lblErgebnis.Text = "Du musst noch:" + Environment.NewLine + monate / 12 + " Jahre oder" + Environment.NewLine + monate + " Monate sparen";
        }
04/23/2011 16:22 xNopex#2
Code:
int jahre = monate / 12;
int restMonate = monate % 12;
lblErgebnis.Text = "Noch " + jahre + " Jahre und " + restMonate + " Monate";
oder, wenn du das als Kommazahl haben möchtest:

Code:
double zeit = monate / 12;
04/23/2011 16:22 HardCore.1337#3
Ich würde es so machen (hab keine ahnung von C#)

Code:
schleife
   ...andere code...
  if(Monate==12) 
  {
    Jahr = Jahr + 1;
    Monate = 0
  }
  ...mehr code...
04/23/2011 21:59 nkkk#4
Quote:
Originally Posted by xNopex View Post
Code:
double zeit = monate / 12;
ahh das klappt nicht ^^
wenn "monate" und "12" ein int ist ist das ergebniss auch ein int was dann einen double zugewiesen wird.
du müsstest
Code:
double zeit = monate / 12d;
(mit dem d dahinter)
schreiben.

ich persönich finde es acuh besser eine jahesangabe nicht als kommastelle zu schreiben.
ich würde es so wei nope im ersten code machen nur das ich die jahre und moate in einem neuen objekt speiechere.
Code:
            int gesamtmonate = 66;//dein wert für die monsate
            int monateImJahr = 12;
            var zeit = new { monate = gesamtmonate % monateImJahr, jahre = gesamtmonate / monateImJahr };
            string text = "Du musst noch " + zeit.monate + " monate und " + zeit.jahre + " Jahre warten.";
04/24/2011 10:17 .BritainAndy#5
Dankeschön für die Hilfe :-)
Das mit dem Objekt gefällt mir gut, nur da muss ich mein Wissen noch etwas weiter aufbauen in C# um das genau zu verstehen.

Thread kann geschlossen werden

Andy

€dit:

Ah doch nicht ich habe ne Frage.

Quote:
int gesamtmonate = 66;//dein wert für die monsate
var zeit = new { monate = gesamtmonate % monateImJahr, jahre = gesamtmonate / monateImJahr };
Und zwar, die gesamtmonate die weiß ich ja nicht.
die Monate wie lang man sparen muss (das meinst du als gesamtmonate, oder?) die sollen ja berechnet werden.

Und noch ne frage, was genau bewirkt dieser code:
Quote:
monate = gesamtmonate % monateImJahr
Andy
04/24/2011 11:10 xNopex#6
Quote:
Und noch ne frage, was genau bewirkt dieser code:
Zitat:
monate = gesamtmonate % monateImJahr
Eine einfache Modulo-Rechnung:
Modulo operation - Wikipedia, the free encyclopedia
04/24/2011 12:55 nkkk#7
die gesamtmonate sind dioch das was du in deinem orogö asl monate abspeichert. ich habs in geasmtmonate um benannt weil ich sonst in mionem code "monate = monate % monateImJahr" schreiben müsste was man eig. nicht macht wenn es sich um 2 verschiedene variablen handlet
04/24/2011 17:33 .BritainAndy#8
edit:

ich idiot ich hatte den Code an der falschen stelle.
Nur wenn ich z.b. als Einnahmen 10 euro hinschreibe als Ausgabe 1 euro und als wunschbetrag 27 euro sagt er als ergebnis 2 monate obwohl es doch 3 sind..

Source:

PHP Code:
       {
            
int kontostand Convert.ToInt32(txtEinkommen.Text);
            
int ausgaben Convert.ToInt32(txtAusgaben.Text);
            
int gewinn = (Convert.ToInt32(txtEinkommen.Text)) - (Convert.ToInt32(txtAusgaben.Text));
            
int wunsch Convert.ToInt32(textBox1.Text);
            
int monateImJahr 12;
            
int gesamtmonate = new int();

            while (
kontostand wunsch)
            {
                
gesamtmonate++;
                
kontostand kontostand gewinn;
            }

            var 
zeit = new { monate gesamtmonate monateImJahrjahre gesamtmonate monateImJahr };
            
string text "Du musst noch " zeit.monate " monate und " zeit.jahre " Jahre warten.";
            
lblErgebnis.Text ""+text+"";
        } 
04/24/2011 18:59 nkkk#9
Quote:
Originally Posted by .BritainAndy View Post
edit:

ich idiot ich hatte den Code an der falschen stelle.
Nur wenn ich z.b. als Einnahmen 10 euro hinschreibe als Ausgabe 1 euro und als wunschbetrag 27 euro sagt er als ergebnis 2 monate obwohl es doch 3 sind..

Source:

PHP Code:
       {
            
int kontostand Convert.ToInt32(txtEinkommen.Text);
            
int ausgaben Convert.ToInt32(txtAusgaben.Text);
            
int gewinn = (Convert.ToInt32(txtEinkommen.Text)) - (Convert.ToInt32(txtAusgaben.Text));
            
int wunsch Convert.ToInt32(textBox1.Text);
            
int monateImJahr 12;
            
int gesamtmonate = new int();

            while (
kontostand wunsch)
            {
                
gesamtmonate++;
                
kontostand kontostand gewinn;
            }

            var 
zeit = new { monate gesamtmonate monateImJahrjahre gesamtmonate monateImJahr };
            
string text "Du musst noch " zeit.monate " monate und " zeit.jahre " Jahre warten.";
            
lblErgebnis.Text ""+text+"";
        } 
ja das stimmt, aber das liegt nicht an meinem code...
ich glaube
Code:
            int kontostand = Convert.ToInt32(txtEinkommen.Text);
ist der fehler, warum ist auf dem kontostand am anfang schon ein einkommen, er sollte null sein.
04/24/2011 20:29 .BritainAndy#10
ich habs jetzt mal durch

Code:
            while (kontostand < wunsch)
            {
                gesamtmonate++;
                kontostand = kontostand + gewinn -1;
            }
gelöst