在C#数组中排列标签输出

时间:2014-02-15 21:39:35

标签: c# arrays string

到目前为止,我有这个代码,并且在正确排列信息输出方面存在问题。我不确定我此刻做错了什么。也许用一个标签会更好

应该看起来像

Price    .10      .15      .20      .25
--------------------------------------
10.00    1.00    1.50     2.00     2.25
15.00    1.50    2.25     3.00     3.25
20.00    2.00    3.00     4.00     5.00
25.00    2.50    3.75     5.00     6.25

但看起来像是

Price       .10      .15      .20      .25
-----------------------------------------------
10.00       2.00    3.00     4.00     5.00       3.00     4.50     6.00    etc. etc.

20.00

30.00

40.00

50.00

我非常感谢您对此的任何帮助

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TippingTable2GUI
{
public partial class frmMain : Form
{
    public frmMain()
    {
        InitializeComponent();
    }

    private void buttonCalc_Click(object sender, EventArgs e)
    {

        double dinnerPrice = Convert.ToDouble(minPrice.Text);
        double tipRate;
        double tip;

        double maxRate = Convert.ToDouble(maxTax.Text);
        double lowRate = Convert.ToDouble(minTax.Text);
        double minDinner = Convert.ToDouble(minPrice.Text);
        double maxDinner = Convert.ToDouble(maxPrice.Text);

        const double TIPSTEP = 0.05;

        const double DINNERSTEP = 10.00;


        tipRate = lowRate;


        label1.Text = "";
        label6.Text = "";
        label7.Text = "";
        label9.Text = "Price";
        for (tipRate = lowRate; tipRate <= maxRate; tipRate += TIPSTEP)

        label1.Text = label1.Text + String.Format("{0, 10}", tipRate.ToString("C"));


label8.Text="--------------------------------------------------------------------------------------";


  while (dinnerPrice <= maxDinner)

          {
  label6.Text = label6.Text + String.Format("{0, -10}\n\n", dinnerPrice.ToString("C"));



  while (tipRate <= maxRate)
           {
   tip = dinnerPrice * tipRate;
   label7.Text = label7.Text + String.Format("{0, 10}", tip.ToString("C"));

   tipRate += 0.05;
                    } 


                    dinnerPrice += DINNERSTEP;
                    tipRate = lowRate;



            }

        }

    }
}

2 个答案:

答案 0 :(得分:0)

阅读string.Format,我相信这可以解决您的问题 该链接直接指向您的问题。但作为一种解决方案......

答案 1 :(得分:0)

private void buttonCalc_Click(object sender, EventArgs e)
{

double dinnerPrice = Convert.ToDouble(minPrice.Text);
double tipRate;
double tip;

double maxRate = Convert.ToDouble(maxTax.Text);
double lowRate = Convert.ToDouble(minTax.Text);
double minDinner = Convert.ToDouble(minPrice.Text);
double maxDinner = Convert.ToDouble(maxPrice.Text);

const double TIPSTEP = 0.05;

const double DINNERSTEP = 10.00;


tipRate = lowRate;


label1.Text = "";
label6.Text = "";
label7.Text = "";

for (tipRate = lowRate; tipRate <= maxRate; tipRate += TIPSTEP)
    label1.Text = label1.Text + String.Format(" {0, 8}", tipRate.ToString("F"))+"\t";\\Use Tab here for proper spacing
label1.Text = label1.Text + String.Format("{0, 8}", tipRate.ToString("C"))+"\t";//Use Tab here for proper spacing
const int NUM_DASHES = 50;
for (int x = 0; x < NUM_DASHES; ++x) ;//Instead of this loop use static dashes like this.

lable2.Text = “” ---------------------------;

while (dinnerPrice <= maxDinner)
{
    label6.Text = label6.Text + String.Format("{0, 8}", dinnerPrice.ToString("C"))+"\t";//Use Tab here for proper spacing
    while (tipRate <= maxRate)
    {
        tip = dinnerPrice * tipRate;
        label7.Text = label7.Text + String.Format("{0, 8}", tip.ToString("F"))+"\t";//Use Tab here for proper spacing
        tipRate += 0.05;
    } 
    dinnerPrice += DINNERSTEP;
    tipRate = lowRate;

}

}