Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Daniel Gonzalez
Daniel Gonzalez
5,312 Points

My code works. But whats wrong with it?

I decided to take a crack at the problem before the instructor gave the solution. The result was a different approach with the same outcome. My code works, but my question is, since its different from the teachers, is there anything wrong with it? I understand there's different ways to solve the same problem but theres always a quicker, cleaner, etc way. With that being said, was my approach at minimal satisfactory? Thank you for your help in advance.

using System;

class Program { static void Main() { double numMinutes = 0; string userInput = "";

    // Prompt user for minutes exercised 
    do{

        Console.Write("Enter how many minutes you exercised: ");

        try{
              userInput = Console.ReadLine();

              if(userInput.ToUpper() != "QUIT"){
                // Add minutes exercised to total
                numMinutes += double.Parse(userInput);
              }

              // Display total minutes exercised to the screen 
              Console.WriteLine(numMinutes);
           }
        catch(FormatException){
              Console.WriteLine("That is not valid Input. Please select a number");
          }
    }
    while(userInput.ToUpper() != "QUIT");

    // Repeat until user quits    
    if(numMinutes < 30){
      Console.WriteLine("Better than nothing. Right?");
    }
    else if (numMinutes > 30 && numMinutes < 60){
      Console.WriteLine("Well done");
    }
    else{
      Console.WriteLine("Now you're just showing off");
    }
}

}

2 Answers

Steven Parker
Steven Parker
230,024 Points

If your code performs the same function, and is similar in efficiency (program size), you can consider your solution equally valid.

As an exercise, a detailed comparison might be of educational value in case some shortcuts or optimizations you may have overlooked were used in the other code.

Daniel Gonzalez
Daniel Gonzalez
5,312 Points

Thank you Steven. I wanted to make sure there weren't any obvious bad practices in the solution.