2015-05-25 173 views
0

C#[4小時新增:)的新品牌],但希望在Board Feet Calculator中限制用戶輸入的指針僅限於數字,不允許使用字母或特殊字符。將用戶輸入限制爲僅限數字

首先,限制發生在類,方法和/或程序中嗎? (我相信類和方法)

第二,我已經看到了一個例子,我會用類似的東西嗎?

第三,如果是這樣,我是否需要爲KeyPress和KeyPressEventArgs分別創建類? (我相信他們會自動出現,例如 public char KeyChar { get; set; }

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    // allows only letters 
    if (!char.IsLetter(e.KeyChar)) 
    { 
     e.Handled = true; 
    } 
} 

我的計劃

namespace BoardFt_MyTry_ 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Board board = new Board(); 

     board.lengthOfboard = Convert.ToDouble(askQuestion("What is the length of your board in inches?")); 
     board.widthOfboard = Convert.ToDouble(askQuestion("What is the width of your board in inches?")); 
     board.thicknessOfboard = Convert.ToDouble(askQuestion("What is the thickness of your board in inches?")); 

     Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt()); 

     Console.ReadLine(); 
    } 
    private static string askQuestion(string question) 
    { 
     Console.WriteLine(question); 
     return Console.ReadLine(); 
    } 

} 

我局級

namespace BoardFt_MyTry_ 
{ 
    class Board 
    { 
     public double lengthOfboard; 
     public double widthOfboard; 
     public double thicknessOfboard; 

    public double CalcBoardFt() 
    { 
     double boardft = 0; 

     boardft = (this.lengthOfboard * this.widthOfboard * this.thicknessOfboard)/144; 

     return boardft; 
    } 
} 
} 
+0

這通常被稱爲「驗證」。 –

+0

不要這樣做 - 讓用戶輸入帶有附加字符的數字,例如空格,逗號,+, - ,然後根據驗證結果過濾輸入或拒絕輸入。即+1,000, - 1 000,1e3,0.1e + 4都可以,1j34不是。 –

+0

謝謝約翰澄清它是「驗證」,以及確認我需要採取不良的輸入,並通知用戶它是壞的。 (我會贊成,但有1名聲望)。 – vayden12

回答

5

你真的不能做,在一個控制檯應用程序。所有你能do是允許用戶輸入不好的數據,然後告訴用戶數據不好。

你可以嘗試這樣的事情:

class Program 
{ 
    public double AskDnoubleQuestion(string message){ 
     do { 
     Console.Write(message); 
     var input = Console.ReadLine(); 

     if (String.IsNullOrEmpty(input)){ 
      Console.WriteLine("Input is required"); 
      continue; 
     } 
     double result; 
     if (!double.TryParse(input, out result)){ 
      Console.WriteLine("Invalid input - must be a valid double"); 
      continue; 
     } 
     return result; 
    } 

    static void Main(string[] args) 
    { 
     Board board = new Board(); 

    board.lengthOfboard = AskDoubleQuestion("What is the length of your board in inches?"); 
    board.widthOfboard = AskDoubleQuestion(askQuestion("What is the width of your board in inches?"); 
    board.thicknessOfboard = AskDoubleQuestion(askQuestion("What is the thickness of your board in inches?"); 

    Console.WriteLine("Your board has {0} board feet.", board.CalcBoardFt()); 

    Console.ReadLine(); 
} 
1

如果確認是不是要繼續的方式,你可以做這樣的事情:

class Program 
{ 
    static void Main(string[] args) 
    { 
     Console.WriteLine("Enter a number:"); 

     string number = ReadNumber(); 
     Console.WriteLine("You entered: " + number); 
    } 

    private static string ReadNumber() 
    { 
     string input = ""; 

     do 
     { 
      ConsoleKeyInfo keyInfo = Console.ReadKey(true); 
      if (char.IsNumber(keyInfo.KeyChar)) 
      { 
       input = input + keyInfo.KeyChar; 
       Console.Write(keyInfo.KeyChar); 
      } 
      if (keyInfo.Key == ConsoleKey.Enter) 
      { 
       Console.WriteLine(); 
       break; 
      } 
      if (keyInfo.Key == ConsoleKey.Backspace) 
      { 
       input = input.Substring(0, input.Length - 1); 
       Console.Write("\b \b"); 
      } 
     } while (true); 

     return input; 
    } 
} 

這將使用戶僅輸入數字。如果你願意,你可以按照你的意願過濾它。例如,只有字母和數字等......

就目前來看,它只允許整數。如果你想允許一個小數點,改變線以上,以這樣的:if (char.IsNumber(keyInfo.KeyChar) || keyInfo.KeyChar == '.')

0

你可以寫,其讀取由關鍵(在控制檯中不顯示)密鑰的方法,忽略了非數字字符,打印有效字符並追加到一個StringBuilder實例,像這樣:

public static string ReadOnlyNumbers() 
{ 
    StringBuilder input = new StringBuilder(); 
    ConsoleKeyInfo ckey; 

    while ((ckey = Console.ReadKey(true)).Key != ConsoleKey.Enter) 
    { 
     if (Char.IsDigit(ckey.KeyChar)) 
     { 
      Console.Write(ckey.KeyChar); 
      input.Append(ckey.KeyChar); 
     } 

     if (ckey.Key == ConsoleKey.Backspace) 
     { 
      input.Length--; 
      Console.Write("\b \b"); 
     } 
    } 

    Console.Write(Environment.NewLine); 
    return input.ToString(); 
} 

然後,您可以使用這樣的:

string input = ReadOnlyNumbers(); 
Console.WriteLine(input);