2015-09-22 88 views
0

我遇到格式問題。但是,我似乎無法讓它看起來合適。tippingtable2gui - 格式化問題C#

我被告知在進入嵌套while循環或僅使用for循環之前初始化tipRate。但仍然遇到格式問題。

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; 
      } 
      } 

     } 

    } 

回答

0

那麼,你應該在僞寫了這一點,那麼僞代碼轉換成實際的代碼。您想要循環所有可能的價格,並且通過所有可能的小費百分比來循環每個價格。你在哪裏「初始化」你的變量很重要。

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 
+0

好的,是否可以修復輸出結果的格式?這確實有幫助,但我真的只需要弄清楚如何讓輸出正確對齊 –