2012-01-15 4 views
1

私はC#を初めて使用しているので、すでに書いたコードで検証する方法を理解することはできません。私は完璧に動作するコードを持っているが、機能を追加し続けたい。私はヒントやあなたが言及したい気になるものを探しています。早めにありがとう。 これまで私が持っていたコードで、緑のコメントの近くの3つのgetInputsで検証が必要です。ここで初心者のためのC#を使用した数字と文字の検証

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

namespace BasicUserInterface 
{ 
    class Program 
    { 
     static void DisplayApplicationInformation() 
     { 
      Console.WriteLine("Welcome to the Basic User Interface Program"); 
      Console.WriteLine("CIS247, Week 2 Lab"); 
      Console.WriteLine("Name: Fred Ziyad"); 
      Console.WriteLine(); 
      Console.WriteLine("This program accepts user input as a string, then makes the"); 
      Console.WriteLine("approppriate data conversion and display the results."); 
      Console.WriteLine(); 
     } 

     static void DisplayDivider(String outputTitle) 
     { 
      Console.WriteLine("************* " + outputTitle + " **************"); 
     } 

     static string GetInput(string inputType) 
     { 
      string strInput; 

      Console.Write("Enter " + inputType + ": "); 
      strInput = Console.ReadLine(); 

      return strInput; 
     } 

     static void TerminateApplication() 
     { 
      Console.WriteLine(); 
      Console.WriteLine("Thank you for using the Basic User Interface program"); 
      Console.Read(); 
     } 

     static void Main(string[] args) 
     { 
      int age; 
      double mileage; 
      string strInput, name; 

      DisplayApplicationInformation(); 

      DisplayDivider("Start Program"); 
      Console.WriteLine(); 

      DisplayDivider("Get Name"); 
      name = GetInput("your name"); 
      Console.WriteLine("Your name is " + name); 
      Console.WriteLine(); 
      //Validate name to be a string of letters. 

      DisplayDivider("Get Age"); 
      strInput = GetInput("your age"); 
      age = int.Parse(strInput); 
      Console.WriteLine("Your age is: " + age); 
      Console.WriteLine(); 
      //Validate age to be a number. 

      DisplayDivider("Get Mileage"); 
      strInput = GetInput("gas mileage"); 
      mileage = double.Parse(strInput); 
      Console.WriteLine("Your car MPT is: " + mileage); 
      //Validate mileage to be a number. 

      TerminateApplication(); 
     } 
    } 
} 

答えて

1
//Validate age to be a number. 

あなたは、文字列が数値であるかどうかを検証する方法は次のとおりです。

string str = "123"; 
int result; 
if (!Int32.TryParse(str, out result)) 
    ; //not a whole number 

TryParseは、文字列が正常に有効な整数のようにして解析されない場合はfalseを返し、そして、場合しますを正常に解析し、変換されたintをoutパラメータに返します。

それとも、小数許可する場合:

string str = "123.5"; 
double result; 
if (!Double.TryParse(str, out result)) 
    ; //not a number 

同じ考え


//Validate name to be a string of letters. 

をここでは、文字でない文字列の文字数を数える方法は次のとおりです。

string str = "AB3C"; 
int numberOfNonLetters = str.Count(c => !Char.IsLetter(c)); 

はちょうどnumberOfNonLettersがゼロ

+0

? (たぶんあなたはブロンコスの遊びを見ているでしょう)。 –

+0

@SteveWellens - 私は完全にAFCの試合を見ているし、2歳の後に気を配っているので、誤字が予想される。-TOUCHDOWN –

+0

ありがとう。 –

3

数値型は、あなたが違法な入力をキャッチするために使用することができますTryParseメソッドを持っていることを確認し、文字列は文字だけを持っていることを確認します。

例:あなたはTryParseを意味するものではありません。

DisplayDivider("Get Age"); 
strInput = GetInput("your age"); 
if (int.TryParse(strInput, out age)) { 
    Console.WriteLine("Your age is: " + age); 
    Console.WriteLine(); 
} else { 
    Console.WriteLine("Age input was not a valid number."); 
} 
+0

ホープあなたは気にしないでください:) –

+0

助けてくれてありがとう、私はこれについて3時間考えてきました。ありがとうございました! –