2013-10-14 162 views
-4

我是C#的2周nube。我試圖讓用戶輸入一個數值(IE:你想要多少?),然後取這個值並計算稅和總額。我無法弄清楚如何做到這一點,並想知道是否有人可以告訴我?謝謝,我現在的腳本是波紋管:處理用戶輸入(Mathmatical)

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Console.WriteLine("Welcome to Infinate Happiness Ranch.\nPlease enter your order information bellow. "); 

      Console.WriteLine(); 

      Console.WriteLine("Please enter your first and last name:"); 
      string FirstName = Console.ReadLine(); 

      Console.WriteLine("Please enter your street address:"); 
      string Address = Console.ReadLine(); 

      Console.WriteLine("Please enter your city:"); 
      string City = Console.ReadLine(); 

      Console.WriteLine("Please enter your two letter state abreviation:"); 
      string StateCode = Console.ReadLine(); 

      Console.WriteLine("Please enter your zip code:"); 
      string ZipCode = Console.ReadLine(); 

      Console.WriteLine("Please enter the number of Tribbles \nyou wish to purchase for $29.99 plus tax"); 
      string NumberOrdered = Console.ReadLine(); 

      Console.WriteLine("Invoice \nName {0}", FirstName); 
      Console.WriteLine("Address {0}", Address); 
      Console.WriteLine("City {0}", City); 
      Console.WriteLine("StateCode {0}", StateCode); 
      Console.WriteLine("ZipCode {0}", ZipCode); 
      Console.WriteLine("NumberOrdered {0}", NumberOrdered); 
      // PROGRAM WORKS UNTIL HERE. 
      NumberOrdered = m; 
      TotalBeforeTax = m * 29.99; //'n' is total b4 tax 
      o = n * 0.9;// 'o' is total tax due 
      p = o + n; // 'p' is total due 

      Console.WriteLine("Your total is {0} {1}", n); 
      Console.WriteLine("Your tax is {0}", o); 
      Console.WriteLine("Your total charge is {0}", p); 
      Console.WriteLine("Thank you for your order"); 
      Console.WriteLine(); 

      //Console.WriteLine("Name:" + FirstName); 
      Console.Read();  
     } 
    } 
} 
+3

你的程序不能編譯。永遠不要說你的程序「不起作用」。解釋你期望發生什麼,以及發生了什麼。 –

+0

什麼是'm'?我沒有看到它的定義。 –

+0

這是一個相當長的程序。也許你可以刪除所有不必要的東西,比如「使用」等? –

回答

-3

只是一些建議,您可以在Console.WriteLine非字符串變量(),如:

Console.WriteLine("Your tax is " + o); 

這是最專業的開發商做。不需要複雜的C/C++風格的解析。

另外,你似乎沒有聲明變量o和p。試試這個:

double o = Convert.toDouble(n * 0.9);

double o =(double)(n * 0.9);

+0

這是一個相當大的泛化...沒有理由引導人們遠離字符串格式的方法。 – RQDQ

+0

有很多。這是一種更直觀的編碼方式,使其更容易理解並減少人爲錯誤的可能性,特別是在處理不同類型的多個變量時。 – user2450099

+0

不好的建議。不回答這個問題。 –

0

你應該解析字符串

int ordered = int.Parse(NumberOrdered); 

,並繼續與這個整數來計算。

0

由於用戶以字符串形式輸入信息,因此應該將訂購的數字轉換爲整數。另外,爲了保留小數點,您需要將數字存儲爲諸如此類數量的雙精度數。

int numOrdered = Convert.ToInt32(NumberOrdered); 
double TotalBeforeTax = numOrdered * 29.99; 

double beforeTax = TotalBeforeTax * 0.9; 
double afterTax = beforeTax + TotalBeforeTax; 

Console.WriteLine("Your total is {0}", TotalBeforeTax); 
Console.WriteLine("Your tax is {0}", beforeTax); 
Console.WriteLine("Your total charge is {0}", afterTax); 
Console.WriteLine("Thank you for your order"); 
0

您忘記聲明一些變量並分配一些值。

試試這個:

static float m; 
static float n; 
static float o; 
static float p; 
static float TotalBeforeTax; 
static void Main(string[] args) 
{ 
    Console.WriteLine("Welcome to Infinate Happiness Ranch.\nPlease enter your order information bellow. "); 
    Console.WriteLine(); 

    Console.WriteLine("Please enter your first and last name:"); 
    string FirstName = Console.ReadLine(); 

    Console.WriteLine("Please enter your street address:"); 
    string Address = Console.ReadLine(); 

    Console.WriteLine("Please enter your city:"); 
    string City = Console.ReadLine(); 

    Console.WriteLine("Please enter your two letter state abreviation:"); 
    string StateCode = Console.ReadLine(); 

    Console.WriteLine("Please enter your zip code:"); 
    string ZipCode = Console.ReadLine(); 

    Console.WriteLine("Please enter the number of Tribbles \nyou wish to purchase for $29.99 plus tax"); 
    string NumberOrdered = Console.ReadLine(); 

    Console.WriteLine("Invoice \nName {0}", FirstName); 
    Console.WriteLine("Address {0}", Address); 
    Console.WriteLine("City {0}", City); 
    Console.WriteLine("StateCode {0}", StateCode); 
    Console.WriteLine("ZipCode {0}", ZipCode); 
    Console.WriteLine("NumberOrdered {0}", NumberOrdered); 
    //PROGRAM WORKS UNTIL HERE ? HELP ? ? ? ? ? 
    //NumberOrdered = m; 
    m = float.Parse(NumberOrdered); 
    TotalBeforeTax = m * 29.99f; //'n' is total b4 tax 
    n = TotalBeforeTax; 
    o = n * 0.9f;//'o' is total tax due 
    p = o + n; //'p' is total due 

    Console.WriteLine("Your total is {0}", n); 
    Console.WriteLine("Your tax is {0}", o); 
    Console.WriteLine("Your total charge is {0}", p); 
    Console.WriteLine("Thank you for your order"); 
    Console.WriteLine(); 
    Console.Read(); 
} 

希望這有助於!