try catch块问题

问题描述:

我遇到的问题是我每次环绕(要求用户输入信息)与尝试捕捉时间,所以如果他们输入了错误的东西或推错误输入将抛出一个错误。当我把尝试捕捉它不起作用它只是结束程序。如果我离开的尝试捕捉出像它是现在和用户只需按下输入崩溃程序..try catch块问题

namespace MaxHeartRate 

{

class Program 
{ 

    static void Main(string[] args) 
    { 
     // Display program instructions 
     DisplayInstructions(); 

     // Collected user info 
     CollectUserInfo(); 

    } 
    static void DisplayInstructions() 
    { 
     Console.WriteLine("************************************************"); 
     Console.WriteLine("This program will Calculate your Max Heart Rate"); 
     Console.WriteLine("Also your Mininum and Maximum target Heart rate"); 
     Console.WriteLine("You will have to enter your info in when promted"); 
     Console.WriteLine("************************************************"); 
     return; 
    } 
    static void CollectUserInfo() 
    { 
     // Declar Variables 
     string firstName, lastName; 
     int birthYear = 0; 
     int currentYear = 0; 
     int age; 
     double maxHeartRate, minTargetHeartRate, maxTargetHeartRate; 

     try 
     { 
      // User is asked to enter info 
      Console.Write("Enter your first Name: "); 
      firstName = Console.ReadLine(); 
      Console.Write("Enter your last name: "); 
      lastName = Console.ReadLine(); 
      Console.Write("Enter the current year: "); 
      currentYear = int.Parse(Console.ReadLine()); 
      Console.Write("Enter your birth year: "); 
      birthYear = int.Parse(Console.ReadLine()); 


      // Find Age 
      age = findAge(currentYear, birthYear); 

      // Find Max Heart rate 
      maxHeartRate = findMaxHR(age); 

      // Find Minimum Target Heart Rate 
      minTargetHeartRate = findMinTHR(maxHeartRate); 

      // Find Maximum Target Heart Rate 
      maxTargetHeartRate = findMaxTHR(maxHeartRate); 

      // Display Information 
      DisplayInformation(firstName, lastName, age, maxHeartRate, minTargetHeartRate, maxTargetHeartRate); 
     } 
     catch (Exception) 
     { 

      Console.WriteLine("Invalid input. Please try again"); 
     } 

     // Methods 
    } 
    static int findAge(int cYear, int bYear) 
    { 
     int age = cYear - bYear; 
     return age; 
    } 
    static int findMaxHR(int age) 
    { 
     int MHR = 220 - age; 
     return MHR; 
    } 
    static double findMinTHR(double maxHeartRate) 
    { 
     double minTHR = maxHeartRate * 0.50; 
     return minTHR; 
    } 
    static double findMaxTHR(double maxHeartRate) 
    { 
     double maxTHR = maxHeartRate * 0.85; 
     return maxTHR; 
    } 
    // Display information 
    static void DisplayInformation(string firstName, string lastName, int age, double maxHeartRate, double minTargetHeartRate, double maxTargetHeartRate) 
    { 
     Console.WriteLine(); 
     Console.WriteLine("************************************************"); 
     Console.WriteLine("Hello " + firstName + " " + lastName + " You are " + age + " Years old" + 
      "\n" + "\nYour Max Heart rate is " + maxHeartRate + 
      " BPM" + "\n" + "\nYour Minimum Target Heart rate is " + minTargetHeartRate + " BPM" + 
      "\n" + "\nwith a Maximum Target Heart rate of " + maxTargetHeartRate + " BPM"); 
     Console.WriteLine("************************************************"); 
     Console.Write("Push enter to exit"); 
     Console.ReadKey(); 
    } 
} 

}

+0

如果你与周围的try-catch代码如果发生错误,将跳转到catch,所以你需要以某种方式回去再问一次。另外,你可以使用'Int.TryParse'来避免try/catch – Gusman

+3

如果你需要try/catch的帮助,为什么不告诉我们你是如何实现它的? – oerkelens

+1

使用调试器通常会晃动。 –

首先,你要拥有的try/catch内要么定义currentYear(包括尝试和catch由于这样的编译器解释代码路径),或在声明。否则,你会在该行获得CS0165: Use of unassigned local variable 'currentYear'age = findAge(currentYear, birthYear)

下面的代码工作:

static void CollectUserInfo() 
{ 
    // Declar Varibals 
    string firstName, lastName; 

    // We can define these here so that there isn't a compiler error. 
    int birthYear = 0; 
    int currentYear = 0; 
    int age; 
    double maxHeartRate, minTargetHeartRate, maxTargetHeartRate; 

    // User is asked to enter info 
    Console.Write("Enter your first Name: "); 
    firstName = Console.ReadLine(); 
    Console.Write("Enter your last name: "); 
    lastName = Console.ReadLine(); 
    try 
    { 
     Console.Write("Enter the current year: "); 
     currentYear = int.Parse(Console.ReadLine()); 
     Console.Write("Enter your birth year: "); 
     birthYear = int.Parse(Console.ReadLine()); 
    } 
    catch (Exception) 
    { 
     // The result of the error. 
     Console.WriteLine("Invalid input."); 
     CollectUserInfo(); 
    } 
    // Find Age 
    age = findAge(currentYear, birthYear); 

    // Find Max Heart rate 
    maxHeartRate = findMaxHR(age); 

    // Find Minimum Target Heart Rate 
    minTargetHeartRate = findMinTHR(maxHeartRate); 

    // Find Maximum Target Heart Rate 
    maxTargetHeartRate = findMaxTHR(maxHeartRate); 

    // Display Information 
    DisplayInformation(firstName, lastName, age, maxHeartRate, minTargetHeartRate, maxTargetHeartRate); 

// Methods 
} 
+0

谢谢。我现在看到我有什么错误了。我首先尝试用尝试抓住包围整个事情。然后,我确实包围了当前的年份和生日,但它抛出了错误。我看到声明变量一起引起了一个问题..感谢帮助家伙.. – Maverick

下面是例子,你怎么能从用户重试尝试获得有效输入:

int currentYear; 
while (true) 
{ 
    var currentYearText = Console.ReadLine(); 
    if (int.TryParse(currentYearText, out currentYear)) 
    { 
     // User entered a valid integer 
     // Validating that integer 
     if (currentYear > 2000 && currentYear < 2050) 
     { 
      break; 
     } 
    } 

    Console.Write("Please enter valid year between 2000 and 2050"); 
} 

如果用户输入无效数据,他会被要求尝试再次。如果用户输入有效,代码将退出循环并转到下一个语句。