Search code examples
c#visual-studio-2015console.writeline

How to print a double using Console.WriteLine in C#


public class Account
   {
       private double balance;

       public Account(double initBalance)
       {
           balance = initBalance;
           this.balance = balance;
       }

       public double getBalance()
       {
           return balance;
       }

       public void deposit(double atm)
       {
           balance = atm + balance;
       }

       public void withdraw(double atm)
       {
           balance = balance - atm;
       }
   }
}
public class TestAccount
   {
       static void other()
       {
           Account objeto = new Account(50.0);

           objeto.getBalance();
           objeto.deposit(100.0);
           objeto.withdraw(147.0);

           Console.WriteLine(objeto.getBalance());
       }
   }

I'm trying to print the balance using writeline, but the output doesn't show anything. I tried writing only text, but it only shows this:

image

What could be happening? I'm using visual studio 2015


Solution

  • To create a console application you need to have a Main function. You are not calling your other method from anywhere now. Main function is the starting point for your application.

    namespace HelloWorld
    {
      class Hello 
      {         
        static void Main(string[] args)
        {
            System.Console.WriteLine("Hello World!");
        }
      }
    }
    

    See how the write line is inside the Main method? You have to write you code there and call methods or create instances there.