עבור לתוכן

בעיה במתודה C#

Featured Replies

פורסם

יש לי תרגיל ויש לי בעיה במתודה מסויימת, המתודה:

returns the sum of object's money value and amount

בהדפסה אני מקבל 0 (הקריאה למתודה היא בסוף ה-main), השם שלה הוא public Money plus(decimal amount)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace T280
{
public class Money
{
public int Dollars;
public int Cents;


public void setMoney(int dollars, int cents)
{
this.Dollars = dollars;
this.Cents = cents;
AddChange();
}


//convert all cents to dollars
public void AddChange()
{
this.Dollars += this.Cents / 100;
this.Cents %= 100;
}


//copies the Money from m
public void setMoney(Money m)
{
this.Dollars = m.Dollars;
this.Cents = m.Cents;
}


//convert the money into decimal
public decimal toDecimal()
{
decimal d = this.Dollars;
decimal c = this.Cents;
c = c / 100;
decimal m = d + c;


return m;
}


//add m to the object's money
public void add(Money m)
{
this.Dollars += m.Dollars;
this.Cents += m.Cents;
AddChange();
}


//add d to object's money
public void add(decimal d)
{
this.Cents += (int)(d * 100);
this.AddChange();
}


//returns the addition of both Money's, but does not affect the object's money value
public Money plus(Money m)
{
Money AddMoney = new Money();
AddMoney.Dollars += this.Dollars + m.Dollars;
AddMoney.Cents += this.Cents + m.Cents;
AddMoney.AddChange();


return AddMoney;
}


//returns the sum of object's money value and amount
public Money plus(decimal amount)
{
Money PlusMoney = new Money();

PlusMoney.add(amount);
this.AddChange();


return PlusMoney;
}
}


class Program
{
static void Main(string[] args)
{
Console.Write("Enter Dollars: ");
int dollar = int.Parse(Console.ReadLine());


Console.Write("Enter Cents: ");
int cents = int.Parse(Console.ReadLine());


Money MyMoney = new Money();
MyMoney.setMoney(dollar, cents);
Console.WriteLine("The Balance Is: " + MyMoney.toDecimal() + "$");


Console.Write("Enter Numbers Of Money You Want To Add: ");
MyMoney.add(int.Parse(Console.ReadLine()));


Console.WriteLine("The New Balance Is: " + MyMoney.toDecimal() + "$");


Money MyMoney2 = new Money();
MyMoney2.add(5);
Console.WriteLine("The New Balance Is: " + MyMoney2.toDecimal() + "$");


Console.WriteLine("-------------------------------------------------");


Money MyMoney3 = new Money();
Console.Write("Enter Numbers Of Money You Want To Add To Object: ");
MyMoney3.plus(int.Parse(Console.ReadLine()));
Console.WriteLine(MyMoney3.toDecimal());


Console.ReadLine();
}
}
}

פורסם

המתודה plus לא משנה את האובייקט, אלא מחזירה אובייקט חדש (בניגוד ל-add, שכן משנה את האובייקט). אתה צריך לשמור את התוצאה איפשהו.

פורסם
  • מחבר

שיניתי את המתודה, איך אני מכניס את זה ל-RETURN ?

    //returns the sum of object's money value and amount
public Money plus(decimal amount)
{
Money PlusMoney = new Money();

this.Cents += (int)(amount * 100) + this.Dollars * 100;
this.AddChange();


return PlusMoney;
}

אם אפשר אז גם הסבר מה בדיוק ההבדל בין 2 המתודות הבאות (ההסבר לפני כל מתודה בהערה זה בדיוק איך שהופיע בתרגיל):

    //returns the addition of both Money's, but does not affect the object's money value
public Money plus(Money m)
{
Money AddMoney = new Money();
AddMoney.Dollars += this.Dollars + m.Dollars;
AddMoney.Cents += this.Cents + m.Cents;
AddMoney.AddChange();


return AddMoney;
}


//returns the sum of object's money value and amount
public Money plus(decimal amount)
{
Money PlusMoney = new Money();

this.Cents += (int)(amount * 100) + this.Dollars * 100;
this.AddChange();


return PlusMoney;
}

פורסם

לא היית צריך לשנות את המתודה, היא הייתה בסדר גמור. היית צריך לשנות את אופן השימוש בה (כמו שאמרתי - לשמור את התוצאה שלה במשתנה חדש).

פורסם
  • מחבר

כן אבל לא ממש הצלחתי, אם תוכל להראות לי אני אשמח

פורסם
  • מחבר

אני עדיין מקבל 0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace T280
{
public class Money
{
public int Dollars;
public int Cents;


public void setMoney(int dollars, int cents)
{
this.Dollars = dollars;
this.Cents = cents;
AddChange();
}


//convert all cents to dollars
public void AddChange()
{
this.Dollars += this.Cents / 100;
this.Cents %= 100;
}


//copies the Money from m
public void setMoney(Money m)
{
this.Dollars = m.Dollars;
this.Cents = m.Cents;
}


//convert the money into decimal
public decimal toDecimal()
{
decimal d = this.Dollars;
decimal c = this.Cents;
c = c / 100;
decimal m = d + c;


return m;
}


//add m to the object's money
public void add(Money m)
{
this.Dollars += m.Dollars;
this.Cents += m.Cents;
AddChange();
}


//add d to object's money
public void add(decimal d)
{
this.Cents += (int)(d * 100);
this.AddChange();
}


//returns the addition of both Money's, but does not affect the object's money value
public Money plus(Money m)
{
Money AddMoney = new Money();
AddMoney.Dollars += this.Dollars + m.Dollars;
AddMoney.Cents += this.Cents + m.Cents;
AddMoney.AddChange();


return AddMoney;
}


//returns the sum of object's money value and amount
public Money plus(decimal amount)
{
Money PlusMoney = new Money();


PlusMoney.add(amount);
this.AddChange();


return PlusMoney;
}
}


class Program
{
static void Main(string[] args)
{
Console.Write("Enter Dollars: ");
int dollar = int.Parse(Console.ReadLine());


Console.Write("Enter Cents: ");
int cents = int.Parse(Console.ReadLine());


Money MyMoney = new Money();
MyMoney.setMoney(dollar, cents);
Console.WriteLine("The Balance Is: " + MyMoney.toDecimal() + "$");


//Console.Write("Enter Numbers Of Money You Want To Add: ");
//MyMoney.add(int.Parse(Console.ReadLine()));


//Console.WriteLine("The New Balance Is: " + MyMoney.toDecimal() + "$");


//Money MyMoney2 = new Money();
//MyMoney2.add(5);
//Console.WriteLine("The New Balance Is: " + MyMoney2.toDecimal() + "$");


Console.WriteLine("-------------------------------------------------");


Money MyMoney3 = new Money();
Console.Write("Enter Numbers Of Money You Want To Add To Object: ");
Money x = MyMoney3.plus(int.Parse(Console.ReadLine()));
//Money x = MyMoney3.plus(3);
Console.WriteLine(MyMoney3.toDecimal());


Console.ReadLine();
}
}
}

פורסם

:facepalm:

כי אתה עדיין מדפיס את MyMoney3 במקום את המשתנה החדש. מה ציפית שיקרה?

פורסם
  • מחבר

טמבל :kopfpatsch:

אגב לפי ההסבר של המתודה אני לא אמור לקבל את המשתנה החדש בנוסף למה שכבר יש לי באובייקט ?

פורסם
  • מחבר

אני לא ממש הבנתי מה בדיוק המתודה הזו אמורה לעשות לכן שאלתי אותך למעלה יותר מה בדיוק ההבדל בין 2 מתודות מסויימות, אשמח להסבר

פורסם

ההבדל אמור להיות די ברור - אחת מקבלת כפרמטר אובייקט מטיפוס Money והאחרת מקבלת decimal.

פורסם
  • מחבר

הבנתי תודה

ארכיון

דיון זה הועבר לארכיון ולא ניתן להוסיף בו תגובות חדשות.

דיונים חדשים