1

そこで、このテキストを書式設定して、さまざまなアドレスのサイズや名前の長さに依存せずに、パディングされ、すべてきれいで正方形になるようにしました。ただし、この形式になったので、実際の変数と値に関しては何も表示されません。

私がする前に

  Console.WriteLine(emp[i].GetEmpName()); 

そしてそれはうまくいきました、それはそのメソッドに保存された文字列を表示しました。切断がどこにあるかはわかりませんが、次のような空白の値が表示されています

Employee Name:
EmployeeAddress:

など。

for (int i = 0; i < count; i++)
        {
            string eNum = "Employee Number: ";
            string eName = "Emplyee Name: ";
            string eAddress = "Employee Address: ";


            Console.WriteLine("------------------------------------------------------------------------");
            Console.Write("|");
            Console.Write(eNum.PadRight(30), emp[i].GetEmpNum());
            Console.Write("|\n");

            Console.Write("|");
            Console.Write(eName.PadRight(30), emp[i].GetEmpName());
            Console.Write("|\n");

            Console.Write("|");
            Console.Write(eAddress.PadRight(30), emp[i].GetEmpAddress());
            Console.Write("|\n");
            //Console.Write();
            //Console.Write();
            Console.Write("|");
            Console.Write("Net Pay: {0:f2}", emp[i].CalcSalary());
            Console.WriteLine("\n------------------------------------------------------ ------------------\n\n");
        }

残りのコードは、見たい場合はここにあります。ここでも、出力をフォーマットしない限り機能します。

私はどこでそんなにひどく間違ったのですか?!?!

using System;
using System.IO;

namespace Proj10
{
class Program
{
    const int ARRAY_TWO = 2;

    static void Main()
    {
        int count = 0;

        Employee[] emp = new Employee[10];

        string environment = System.Environment.GetFolderPath
        (System.Environment.SpecialFolder.Personal) + "\\";

        Console.WriteLine("Enter a file name in My Documents: ");
        string input = Console.ReadLine();

        string path = environment + input;

        StreamReader myFile = new StreamReader(path);


        do
        {

            if (myFile.EndOfStream)
            {
                break;
            }
            int num = int.Parse(myFile.ReadLine());
            string name = myFile.ReadLine();
            string address = myFile.ReadLine();
            string hourNworked = myFile.ReadLine();
            double[] values = new double[ARRAY_TWO];
            sort(hourNworked, ref values);

            emp[count] = new Employee();
            emp[count].SetEmpNum(num);
            emp[count].SetEmpName(name);
            emp[count].SetEmpAddress(address);
            emp[count].SetEmpHrlyPay(values[0]);
            emp[count].SetEmpHrsWrked(values[1]);



            //while count < 10 && !myfile.EOF();

            //print loop using count 
            //emp[0].GetEmpNum();
            //emp[0].CalcSalary();

            /*Integer[] number = new Integer[5];
            for (int i = 0; i < 5; i++)
                number[i] = new Integer(i);
            for (int i = 0; i < 5; i++)
                Console.WriteLine(number[i].GetValue());
            */

            count++;


        } while (count < 10);

        Console.WriteLine("\n\nAuthor: Daniel Demers");
        Console.WriteLine("CS 1400 X01\n");

        for (int i = 0; i < count; i++)
        {
            string eNum = "Employee Number: ";
            string eName = "Emplyee Name: ";
            string eAddress = "Employee Address: ";


            Console.WriteLine("------------------------------------------------------------------------");
            Console.Write("|");
            Console.Write(eNum.PadRight(30), emp[i].GetEmpNum());
            Console.Write("|\n");

            Console.Write("|");
            Console.Write(eName.PadRight(30), emp[i].GetEmpName());
            Console.Write("|\n");

            Console.Write("|");
            Console.Write(eAddress.PadRight(30), emp[i].GetEmpAddress());
            Console.Write("|\n");
            //Console.Write();
            //Console.Write();
            Console.Write("|");
            Console.Write("Net Pay: {0:f2}", emp[i].CalcSalary());
            Console.WriteLine("\n------------------------------------------------------------------------\n\n");
        }

        Console.ReadLine();
    }

    public static void sort(string hourNworked, ref double[] values)
    {
        char[] rules = { ' ' };
        string[] splitArray = hourNworked.Split(rules);
        values[0] = double.Parse(splitArray[0]);
        values[1] = double.Parse(splitArray[1]);
    }

    public class Employee
    {
        private int empNum;
        private string name;
        private string address;
        private double hrlyPay;
        private double hrsWrkd;
        private double GROSS;
        private double overTime;
        private double overTimePay;
        private double overTimeHours;
        private double NET;
        private double regTime;
        private double fTaxAmount;
        private double sTaxAmount;

        private const double FED_TAX = .20;
        private const double STATE_TAX = .075;
        private const double OT_MULTIPLYER = 1.5;
        private const int FORTY_HOURS = 40;


        public Employee()
        {
            empNum = 0;
        }

        public void SetEmpNum(int n)
        {
            empNum = n;
        }

        public int GetEmpNum()
        {
            return empNum;
        }
        public void SetEmpName(string n)
        {
            name = n;
        }
        public string GetEmpName()
        {
            return name;
        }
        public void SetEmpAddress(string a)
        {
            address = a;
        }
        public string GetEmpAddress()
        {
            return address;
        }
        public void SetEmpHrlyPay(double h)
        {
            hrlyPay = h;
        }
        public double GetEmpHrlyPay()
        {
            return hrlyPay;
        }
        public void SetEmpHrsWrked(double w)
        {
            hrsWrkd = w;
        }
        public double GetEmpHrsWrkd()
        {
            return hrsWrkd;
        }
        public double CalcSalary()
        {
            if (hrsWrkd > 40)
            {
                overTimeHours = hrsWrkd - FORTY_HOURS;
                overTimePay = hrlyPay * OT_MULTIPLYER;
                overTime = overTimePay * overTimeHours;
                regTime = (hrsWrkd - overTimeHours) * hrlyPay;
                GROSS = overTime + regTime;

                fTaxAmount = (regTime + overTime) * FED_TAX;
                sTaxAmount = (regTime + overTime) * STATE_TAX;

                NET = GROSS - (fTaxAmount + sTaxAmount);

                return NET;
            }
            else
            {
                regTime = hrlyPay * hrsWrkd;
                fTaxAmount = regTime * FED_TAX;
                sTaxAmount = regTime * STATE_TAX;

                NET = regTime - (fTaxAmount + sTaxAmount);

                return NET;
            }
        }
    }
}
}
4

1 に答える 1

4

使用しているConsole.Writeオーバーロードは、書式文字列とオブジェクトを取ります。最初の引数にはプレースホルダーが含まれていないため、2 番目の引数は使用されません。

代わりにこれを試してください:

Console.WriteLine("|{0,-30}{1,-20}|", eNum, emp[i].GetEmpNum());

オンラインでの動作を確認してください: ideone

于 2012-08-11T21:10:23.563 に答える