用于将字符串转换为int的Java if语句

时间:2016-11-02 06:02:18

标签: java

我正在尝试创建一个语句,根据以下内容将字符串转换为int:

very negative = -2
negative = -1 
neutral = 0 
positive= 1 
very positive = 2 

这是我拥有的,并且由于某种原因,每当我调用此函数时,它返回5000,这表示错误:

  public static int convertSentimentToInt(String sentimentString)
  {
      int sentimentInt;

      if (sentimentString == "Very negative")
        sentimentInt = -2;
      else if(sentimentString == "Negative")
          sentimentInt = -1;
      else if(sentimentString == "Neutral")
          sentimentInt = 0;
      else if(sentimentString == "Positive")
          sentimentInt = 1;
      else if(sentimentString == "Very positive")
        sentimentInt = 2;
      else
          sentimentInt = -5000;

      return sentimentInt;
  }

我知道我作为参数传递的字符串是其中一个选项("非常消极","否定"等等),因为我之前测试过它。

0 个答案:

没有答案