View Single Post
Old Nov 18th, 2006, 8:07 AM   #7
DC_FC79
Newbie
 
Join Date: Nov 2006
Posts: 8
Rep Power: 0 DC_FC79 is on a distinguished road
Quote:
Originally Posted by Arevos View Post
Well, I'll give you an example similar to your problem. Lets say you want to create a program that converts Fahrenheit into degrees Celsius. It might look something like this:
c# Syntax (Toggle Plain Text)
  1. using System;
  2.  
  3. namespace Example
  4. {
  5. class FahrenheitCelsiusConverter
  6. {
  7. static void Main(string[] args)
  8. {
  9. // Get input from the user
  10. Console.WriteLine("Enter a temperature in Fahrenheit");
  11. string userInput = Console.ReadLine();
  12.  
  13. // Convert user input into a number
  14. double fahrenheit;
  15. try
  16. {
  17. fahrenheit = double.Parse(userInput);
  18. }
  19. // If the user entered something that wasn't a number
  20. // then complain and exit
  21. catch (FormatException)
  22. {
  23. Console.WriteLine("Invalid input. Exiting!");
  24. return;
  25. }
  26.  
  27. // Convert fahrenheit to celsius using an equation
  28. double celsius = (fahrenheit - 32) / 1.8;
  29.  
  30. Console.WriteLine("The temperature in Celsius is " + celsius);
  31. }
  32. }
  33. }
Note that "double" is a type of number in C# that can have decimal places. If you only need whole numbers (i.e. integers), then you can use "int" instead.
Arevos

ill have a go at changing the bits necessary for me
DC_FC79 is offline   Reply With Quote