将字符串与字符进行比较

时间:2014-03-13 04:52:55

标签: java

我一直在尝试使用3函数在java中创建温度转换器,用于转换华氏度,摄氏度和打印结果。代码有效,但由于原因我将String与Char进行比较,使表达式永远不相等。我真的很困惑。我需要你的帮助,谢谢你。

TemperatureConverter:

import java.util.*;
public class TemperatureConverter {


  public static double convertToCelsius(double degrees ){
    double celsius = (5*(degrees - 32)/9);
    return celsius;
  }


  public static double convertToFahrenheit(double degrees ){
    double fahrenheit = (9*( degrees/5 ) + 32);
    return fahrenheit;
  }



  public static String getOutput(char units, double degrees) { 
    if ((units == 'F') || (units == 'f'))
    {
      String output = " ";
      output = degrees + " F = " + convertToCelsius(degrees);
      return output;
    }
    else if ((units) == 'C' || (units) == 'c' )
    {
      String output = " ";
      output = degrees + " C = " + convertToFahrenheit(degrees);
      return output;
    }
    if(!(units == 'F' || units == 'f' || units == 'C' || units == 'c'))
    {
      String output = ("Unknown units - cannot do calculation\n" + 
                    " - next time enter either \'F\' for Fahrenheit or \'C\' for Celsius.");
      return output;
    }
    return "";
  }

  public static void main(String[] args) { 
    Scanner keyboard = new Scanner(System.in);

    double degrees;
    char units;

    System.out.println("Enter a temperature in degrees (for example 29.6): ");
    degrees = keyboard.nextDouble();

    System.out.println();
    System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");

    units = keyboard.next().charAt(0);
    System.out.println(getOutput(units, degrees)); 
  }
}

TemperatureConverterTest:

import junit.framework.TestCase;

/**
 * A JUnit test case class.
 * Every method starting with the word "test" will be called when running
 * the test with JUnit.
 */
public class TemperatureConverterTest extends TestCase {
  public static final double EPSILON = 0.0001;
  public void testConvertToCelsius() {
    assertEquals(0.0, TemperatureConverter.convertToCelsius(32), EPSILON);
    assertEquals(100, TemperatureConverter.convertToCelsius(212), EPSILON);
    assertEquals(-17.7777777778, TemperatureConverter.convertToCelsius(0), EPSILON);
    assertEquals(-40, TemperatureConverter.convertToCelsius(-40), EPSILON);
  }

  public void testConvertToFahrenheit() {
    assertEquals(32, TemperatureConverter.convertToFahrenheit(0), EPSILON);
    assertEquals(100, TemperatureConverter.convertToFahrenheit(37.7777778), EPSILON);
    assertEquals(0, TemperatureConverter.convertToFahrenheit(-17.7777778), EPSILON);
    assertEquals(-40, TemperatureConverter.convertToFahrenheit(-40), EPSILON);
  }

  public void testGetOutput() {
    String output = TemperatureConverter.getOutput('C', 0.0);
    assertEquals("User inputs 'C' and 0.0 degrees", "0.0 degrees C = 32.0 degrees Fahrenheit.", output);

    output = TemperatureConverter.getOutput('c', 0.0);
    assertEquals("User inputs 'c' and 0.0 degrees", "0.0 degrees C = 32.0 degrees Fahrenheit.", output);

    output = TemperatureConverter.getOutput('F', 0.0);
    assertEquals("User inputs 'F' and 0.0 degrees", "0.0 degrees F = -17.77777777777778 degrees Celsius.", output);

    output = TemperatureConverter.getOutput('f', 0.0);
    assertEquals("User inputs 'f' and 0.0 degrees", "0.0 degrees F = -17.77777777777778 degrees Celsius.", output);

    output = TemperatureConverter.getOutput('m', 0.0);
    assertEquals("User inputs 'm' and 0.0 degrees", "Unknown units - cannot do calculation\n" + 
                " - next time enter either 'F' for Fahrenheit or 'C' for Celsius.", output);
  }
}

3 个答案:

答案 0 :(得分:2)

您的问题不是比较字符串和字符,而是预期的输出与给定的输出不匹配。

你需要添加额外的' guff'在您对输出结果的预期答案中。

将加星标的行(**)添加到' F'分支和类似的代码到' C'分支

if ((units == 'F') || (units == 'f')) {
    **String tempType = "Fahrenheit";**
    String output = " ";
    output = degrees + " F = " + convertToCelsius(degrees);
    **output = output + " degrees " + tempType**
    return output;

答案 1 :(得分:1)

比较字符串和字符。你有这个:

if ((units == 'F') || (units == 'f'))

但是'F'(在单引号中)是字符文字,而不是字符串。如果它是"F"(双引号)那么它将是一个字符串:

char a = 'F';     // <- this is a character 
String b = "F";   // <- this is a string
char c = 'XYZ';   // <- this is invalid code!
String d = "XYZ"; // <- this is a string

我认为你对'F'感到困惑 - 它是一个角色,而不是一个字符串。所以,你的代码看起来很好。

答案 2 :(得分:1)

请尽量使您的功能如下:

  public static String getOutput(char units, double degrees) { 
    if ((units == 'F') || (units == 'f'))
    {
      String output = " ";
      output = degrees + " degrees F = " + convertToCelsius(degrees) + " degrees Celsius.";
      return output;
    }
    else if ((units) == 'C' || (units) == 'c' )
    {
      String output = " ";
      output = degrees + " degrees C = " + convertToFahrenheit(degrees) + " degrees Fahrenheit.";
      return output;
    }
    if(!(units == 'F' || units == 'f' || units == 'C' || units == 'c'))
    {
      String output = ("Unknown units - cannot do calculation\n" + 
                    " - next time enter either \'F\' for Fahrenheit or \'C\' for Celsius.");
      return output;
    }
    return "";
  }

它通过我的电脑测试..

希望它有所帮助..