我的if语句不起作用?语音合成器

时间:2014-06-24 14:25:00

标签: java if-statement speech-synthesis freetts

嘿我在java中编写一个程序,你输入一个控制台,然后它回复计算机实际说话(有声),我使用FreeTTs语音合成器。出于某种原因,当我写下面的代码时,它的输出不是我想要的。

import java.util.Scanner;

import com.sun.speech.freetts.VoiceManager;
import com.sun.speech.freetts.Voice;

public class TextToSpeech {
    public static void main(String args[]){

         Scanner input = new Scanner(System.in);
         String userInput = input.nextLine();

         if(userInput == "hi"){
         Voice v;
         VoiceManager vm=VoiceManager.getInstance();
         v=vm.getVoice("kevin16");
         v.allocate();
         v.speak("Hey my name is jarvis");
         input.close();
         }else
             System.out.println("you suck try again");
    }

}

1 个答案:

答案 0 :(得分:1)

当您比较实例中的引用变量(String引用变量)时,==比较运算符会检查它们是否引用同一对象。例如;

String s = new String("s");
String s2 = "s";
System.out.println(s==s2);

以上输出false因为ss2未在内存中引用相同的String对象;

相反,请使用.equals()方法来比较您的String引用有意义是否相等。例如;

String s = new String("s");
String s2 = "s";
System.out.println(s.equals(s2));

以上输出true