2011-01-05 65 views
0

我有一個控制檯應用程序詢問用戶在一個月內的銷售數據。我已經讓程序拒絕低於零的條目,並要求用戶再次輸入他們的銷售數字。但現在我希望當用戶輸入一個字母或任何其他字符不是數字的代碼我現在有是發生同樣的事情:檢查用戶輸入的無效字符

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

namespace FIRST_ACTUAL_PROJECT 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      FileStream fin; // this is declaring that you are using a filestream. 
      String s; 
      int LineNum = 0; 
      double seventy_percent_value; 
      double thirty_percent_value; 
      const int max_num_of_items = 20; // this means that there will always be a maximum of 20 sales figures because there is a maximum of 20 customers 
      double[] sales_figures = new double[max_num_of_items]; // this is the array for the sales figures 
      string[] customer = new string[max_num_of_items]; // this is the array for the customers 
      double[] licence_fee_in_percent = new double[max_num_of_items]; // this is the array for the licence fee 
      double[] fee_payable = new double[max_num_of_items]; // array for the fees payable in pounds. 
      const double MIN_SALES_FIGURE = 0; 
      try 
      { 
       fin = new FileStream("customer list.txt", FileMode.Open);// this is opening the file. 
      } 
      catch (IOException exc) 
      { 
       Console.WriteLine(exc.Message + "cannot find file!"); // error message if it does'nt find the file or something went wrong. 
       Console.ReadLine(); 
       return; 
      } 
      StreamReader fstr_in = new StreamReader(fin); // this is telling the streamreader which file to read. 
      try 
      { 
       while ((s = fstr_in.ReadLine()) != null) // this is reading the file until the end. 
       { 
        Console.WriteLine(s); 
        customer[LineNum] = s.Split(',')[0]; 
        licence_fee_in_percent[LineNum] = double.Parse(s.Split(',')[1]); 
        LineNum = LineNum + 1; 
       } 
      } 
      catch (IOException exc) 
      { 
       Console.WriteLine(exc.Message); 
      } 
      for (int CustPos = 0; CustPos < LineNum; CustPos = CustPos + 1) // this determines what the loop does. 
      { 
       Console.Write("enter sales figures for" + customer[CustPos] + " "); // this asks the user to enter the sales figures 
       sales_figures[CustPos] = Double.Parse(Console.ReadLine()); // this is user's input is read in and stored. 

       while (sales_figures[CustPos] < MIN_SALES_FIGURE) // this is if the user enters a number below zero. 
       { 
        Console.WriteLine(""); 
        Console.WriteLine("entry invalid"); 
        Console.WriteLine(""); 
        Console.WriteLine("enter sales figures for" + customer[CustPos] + " "); 
        sales_figures[CustPos] = Double.Parse(Console.ReadLine()); 
       } 

        Console.WriteLine(" "); 
        fee_payable[CustPos] = (sales_figures[CustPos]/100.0) * licence_fee_in_percent[CustPos]; 
        Console.WriteLine(customer[CustPos] + " ----------- " + fee_payable[CustPos]); 
        Console.WriteLine("Licence fee to be paid in GBP is :" + fee_payable[CustPos]);   //this section displays the cust name, sales figure 70/30. 
        seventy_percent_value = ((fee_payable[CustPos]/10.0) * 7); 
        Console.WriteLine("70 percent of this fee is" + seventy_percent_value); 
        thirty_percent_value = ((fee_payable[CustPos]/10.0) * 3); 
        Console.WriteLine("30 percent of this fee is" + thirty_percent_value); 
        Console.WriteLine(" "); 
       } 

      } 
      Console.WriteLine("Customer name" + "\t" + "sales" + "\t" + "fee paid" + "\t" + "70% value" + "\t" + "30% value" + "\t"); 
      for (int DisplayPos = 0; DisplayPos < LineNum; DisplayPos = DisplayPos + 1) 
      { 
       seventy_percent_value = ((fee_payable[DisplayPos]/10.0) * 7); 
       thirty_percent_value = ((fee_payable[DisplayPos]/10.0) * 3); 
       Console.WriteLine(customer[DisplayPos] + "\t" + sales_figures[DisplayPos] + "\t" + fee_payable[DisplayPos] + "\t\t" + seventy_percent_value + " \t\t" + thirty_percent_value + "\t"); 
      } 
      Console.WriteLine(" "); 
      Console.WriteLine("Press enter to finish"); 
      Console.ReadLine(); 
     } 
    } 
} 

回答

0

我覺得可能會使用正則表達式來檢查輸入以正確的形式。

4

而不是使用Double.Parse,使用Double.TryParse - 這將返回數字是否被成功解析。

更好的是,使用Decimal.TryParse - 您不應該使用double作爲貨幣值。

補充建議:

  • 修正命名空間一個與.NET命名約定
  • 你已經有了一個巨大的方法符合 - 它分解成幾種方法,每個執行一個小任務
  • 考慮使用List<T>而不是陣列 - 這樣你就不需要預先分配所有的東西
  • 你不會爲你的v有任何特定的命名約定ariables;這將是很好的一致
  • 一般喜歡在第一次使用點聲明局部變量,而不是在方法的頂部宣佈一切
  • 使用using語句關閉您的資源,如河流和讀者。 (目前我不認爲你收什麼,永遠。)
+0

好的會試試,謝謝! – 2011-01-05 13:42:03

+0

double.tryparse不起作用,因爲它說'方法'沒有重載TryParse'需要1個參數'。只是想知道你是否可以解釋,因爲我是一個完整的初學者!謝謝 – 2011-01-05 13:46:55

+0

我認爲這個傢伙來自C++ :) – Sherwin 2011-01-07 01:43:18

0

使用的TryParse,而不是解析:

替換:

sales_figures[CustPos] = Double.Parse(Console.ReadLine()); // this is user's input is read in and stored. 

有了:

bool isValidDouble = Double.TryParse(Console.ReadLine(), out sales_figures[CustPos]); 

然後檢查以後再用isValidDouble。

+0

這個工作,但它只是使用數字零。我想重新詢問用戶的銷售數據 – 2011-01-05 15:14:52

+0

@Stephen:如果您「稍後檢查isValidDouble」,它將不會使用數字零 – 2011-01-07 06:27:09