public class TemperatureConverter
{
public double FahrenheitToCelsius(double fahrenheit)
{
if (fahrenheit < -459.67)
{
throw new ArgumentOutOfRangeException("The Fahrenheit temperature cannot be less than -459.67");
}
return (fahrenheit - 32) * 5 / 9;
}
public double CelsiusToFahrenheit(double celsius)
{
if (celsius < -273.15)
{
throw new ArgumentOutOfRangeException("The Celsius temperature cannot be less than -273.15");
}
return (celsius * 9 / 5) + 32;
}
}
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using YourTemperatureConversionNamespace; // 請將此替換為您的溫度轉換類別的命名空間
namespace TemperatureConversionTests
{
[TestClass]
public class TemperatureConversionTest
{
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestFahrenheitToCelsiusBelowAbsoluteZero()
{
// Arrange
TemperatureConverter converter = new TemperatureConverter();
double inputFahrenheit = -500.0; // Less than absolute zero
// Act
converter.FahrenheitToCelsius(inputFahrenheit);
// Assert is handled by the ExpectedException attribute
}
[TestMethod]
[ExpectedException(typeof(ArgumentOutOfRangeException))]
public void TestCelsiusToFahrenheitBelowAbsoluteZero()
{
// Arrange
TemperatureConverter converter = new TemperatureConverter();
double inputCelsius = -300.0; // Less than absolute zero
// Act
converter.CelsiusToFahrenheit(inputCelsius);
// Assert is handled by the ExpectedException attribute
}
}
}
public class TemperatureConverter
{
public double FahrenheitToCelsius(double fahrenheit)
{
if (fahrenheit < -459.67)
{
throw new ArgumentOutOfRangeException("The Fahrenheit temperature cannot be less than -459.67");
}
return (fahrenheit - 32) * 5 / 9;
}
public double CelsiusToFahrenheit(double celsius)
{
if (celsius < -273.15)
{
throw new ArgumentOutOfRangeException("The Celsius temperature cannot be less than -273.15");
}
return (celsius * 9 / 5) + 32;
}
}