Jump to content

C#


dan_t

Recommended Posts

OK, here's my question, how is this code formatting a table?

  class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Please input your name: ");
            string employeeName = Console.ReadLine();

            Console.Write("Please input your salary: ");
            string inputSalary = Console.ReadLine();
            double salary = int.Parse(inputSalary);

            double balance5Percent = 0.0;
            double balance10Percent = 0.0;
            double balance15Percent = 0.0;

            Console.WriteLine();
            Console.WriteLine("{0} at your current salary of {1:C} these are your expected balances", employeeName, salary);
            Console.WriteLine();
            Console.WriteLine("                    5%              10%              15%");
            Console.WriteLine("=========================================================");
            for (int i = 1; i < 40; i++)
            {
                balance5Percent = (salary * 0.05 + balance5Percent) * 1.08;
                balance10Percent = (salary * 0.10 + balance10Percent) * 1.08;
                balance15Percent = (salary * 0.15 + balance15Percent) * 1.08;

                if ((i % 10) == 0)
                {
                    Console.WriteLine(" Year {0}   {1:C}  {2,14:f}  {3,14:C}", i, balance5Percent, balance10Percent, balance15Percent);
                }
            }
            Console.WriteLine("=========================================================");
            Console.ReadLine();
        }
    }
    }

This is code from a class I am taking, but do not worry, this is not a question from the class. I would just like to know how to restructure it (the table).

In order to restructure I have to understand how it is being structured in the first place.

I could use the help.

Thanks

Link to comment
Share on other sites

To be honest, I'm not sure what you're asking.  The program takes in some input, and calculates the money earned on that data via interest for 39 years.  There's no real formatting per se, as everything is simply spewed back out to standard output.  Each line has some formatting - to currency, or a specifically given floating point precision - but that's it.

 

You should ensure your conversions from string to int to double all work.  The compiler can get picky with that sort of thing.

Link to comment
Share on other sites

using System;

 

using System.Collections.Generic;

 

using System.Text;

 

namespace lab3a

 

{

 

class Program

 

{

 

static void Main(string[] args)

 

{

 

Console.Write("Please input your name: ");

 

string employeeName = Console.ReadLine();

 

Console.Write("Please input your salary: ");

 

string inputSalary = Console.ReadLine();

 

double salary = int.Parse(inputSalary);

 

double balance5Percent = 0.0;

 

double balance10Percent = 0.0;

 

double balance15Percent = 0.0;

 

Console.WriteLine();

 

Console.WriteLine("{0} at your current salary of {1:C} these are your expected balances", employeeName, salary);

 

Console.WriteLine();

 

Console.WriteLine(" 5% 10% 15%");

 

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

 

for (int i = 1; i <= 40; i++)

 

{

 

balance5Percent = (salary * 0.05 + balance5Percent) * 1.08;

 

balance10Percent = (salary * 0.10 + balance10Percent) * 1.08;

 

balance15Percent = (salary * 0.15 + balance15Percent) * 1.08;

 

if ((i % 10) == 0)

 

{

 

Console.WriteLine(" Year {0} {1:C} {2,14:f} {3,14:C}", i, balance5Percent, balance10Percent, balance15Percent);

 

}

 

}

 

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

 

Console.ReadLine();

 

}

 

}

 

}

 

This formats in 5, 10, and 15% across a table, but how do I go 5% for 10, 20, 30 ,40 years across the table(so to speak).

I can't figure it out.

Any help wold be appreciated.

Thank

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.