tippingtable2gui - 格式化问题C#

时间:2015-09-22 15:09:47

标签: c#

我在格式化方面遇到问题。但是,我似乎无法让它看起来不合适。

我被告知在进入嵌套的while循环之前初始化tipRate或仅使用for循环。但我仍然无法格式化。

What it should look like

What mine looks like

这是我的代码

    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 WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonCalc_Click(object sender, EventArgs e)
        {
        // Constants
        const double TIPSTEP = 0.05;
        const double DINNERSTEP = 10.00;    

        // Variables
        double maxRate = Convert.ToDouble (maxTip.Text);
        double lowRate = Convert.ToDouble (minTip.Text);
        double minDinner = Convert.ToDouble (minPrice.Text);
        double maxDinner = Convert.ToDouble (maxPrice.Text);
        double dinnerPrice = Convert.ToDouble (minPrice.Text);
        double tipRate;
        double tip;

        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, 8}", tipRate.ToString("F")) + "\t";
            label1.Text = label1.Text + String.Format("{0, 8}", tipRate.ToString("C")) + "\t";

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

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

        }

    }

1 个答案:

答案 0 :(得分:0)

好吧,你应该用伪代码写出来,然后将伪代码转换为实际的代码。您希望循环遍历所有可能的价格,并且每个价格都会循环显示所有可能的提示百分比。你在哪里&#34;初始化&#34;你的变量很重要。

initialize dinnerprice, maxDinner and maxTip

while dinnerprice <= maxDinner
begin
    set the tiprate to minimum  (This needs to be done at the start of every dinnerprice)

    while tiprate <= maxtiprate
    begin
        calculate tip
        print the tip in the appropriate place
        increment the tiprate
    end

    increment the dinnerprice
end