0
Do you know why this can't run?
can anyone tell me why this code can't run? namespace SoloLearn { class BankAccount { private double balance=0; public void Deposit(double n) { balance += n; sb.append = n; } public void Withdraw(double n) { balance -= n; sb.append = n; } public double GetBalance() { return balance; } } class Program { static void Main(string[] args) { StringBuilder sb
11 ответов
+ 4
It was sb.Append not sb.append 😜
https://code.sololearn.com/c26yYLU7PPT3/?ref=app
+ 3
Just send the StringBuilder sb as another parameter.
+ 3
// Your project but Improved. 😊😉😊
namespace SoloLearn
{
class BankAccount {
private double balance=0;
public void Deposit(double n, StringBuilder sb) {
balance += n;
sb.append("Deposit : " + n.ToString() + "\n");
}
public void Withdraw(double n, StringBuilder sb) {
balance -= n;
sb.append("Withdraw : " + n.ToString() + "\n");
}
public double GetBalance() {
return balance;
}
}
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
BankAccount b = new BankAccount();
b.Deposit(199, sb);
b.Withdraw(42, sb);
string sb1 = sb.ToString();
Console.WriteLine(sb1); Console.WriteLine(b.GetBalance());
}
}
}
+ 2
you are declaring stringbuilder sb inside the main fix it.
+ 1
how do you know your code is not working?
write it in code playground and insert your code in this question.
+ 1
sorry here is the full code.. some text were missing from question
I wrote it in code playground and it didn't work
namespace SoloLearn
{
class BankAccount {
private double balance=0;
public void Deposit(double n) {
balance += n;
sb.append = n;
}
public void Withdraw(double n) {
balance -= n;
sb.append = n;
}
public double GetBalance() {
return balance;
}
}
class Program
{
static void Main(string[] args)
{
StringBuilder sb = new StringBuilder();
BankAccount b = new BankAccount();
b.Deposit(199);
b.Withdraw(42);
string sb1 = sb.ToString;
console.writeline(sb1); Console.WriteLine(b.GetBalance());
}
}
}
+ 1
..\Playground\(34,26): error CS0428: Cannot convert method group 'ToString' to non-delegate type 'string'. Did you intend to invoke the method?
..\Playground\(35,10): error CS0103: The name 'console' does not exist in the current context
+ 1
thanks i have learnt a lot
there were multiple mistakes in my code
1. send sb as parameter
2. sb.Append not append
3. sb.ToString(); needed the ()
4. Console.WriteLine not console.writeline
it works as this
https://code.sololearn.com/c48N8ZA0FI2f/?ref=app
with output
/*
balance =0
+199
-42
balance =157
*/
0
I'm working with sb coz I want to display a string at the end... showing all steps and it will include all deposit and withdrawal records
0
I tried to run your code and got this error
..\Playground\(47,16): error CS1061: 'StringBuilder' does not contain a definition for 'append' and no extension method 'append' accepting a first argument of type 'StringBuilder' could be found (are you missing a using directive or an assembly reference?)
..\Playground\(51,16): error CS1061: 'StringBuilder' does not contain a definition for 'append' and no extension method 'append' accepting a first argument of type 'StringBuilder' could be found (are you missing a using directive or an assembly reference?)
0
luka your code works and it is the default code included in sololearn course, however it doesn't do what I need.. which is writing every transaction done on this bank account.