首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CurrencyConversion (Java)

CurrencyConversion (Java)
EN

Stack Overflow用户
提问于 2015-01-23 12:03:58
回答 2查看 313关注 0票数 0

我的If语句有问题。代码运行时没有任何问题,但是它没有显示if语句的输出。它通过我所有的JOptionboxes一直运行到最后。直到它通过我的if语句。

代码语言:javascript
复制
import javax.swing.JOptionPane;

public class CurrencyConversion {

public static void main(String[] args) {

int x = 0;
double result,result1, result2;
String THAI = "THAI", EURO ="EURO", JPY = "JPY";

String name = JOptionPane.showInputDialog("What is your name: ");
String message = String.format("Welcome %s, to the Currency Exchange Program ", name); /*inputs name in %s*/ 
JOptionPane.showMessageDialog(null,  message); //displays the String format message
String currency = JOptionPane.showInputDialog("Which currency do you wish to exchange: "+THAI+ ", "+EURO+ ", "+JPY);
x = Integer.parseInt (JOptionPane.showInputDialog("Insert US Dollar Amount: "));
result = x * 32.57; //US TO THAI
result1 = x * .86; //US TO EURO
result2 = x * 117.50; //US TO JPY


if (currency == "THAI"){
    JOptionPane.showMessageDialog(null,"US Dollar Amount of "+x+ " dollars to be converted to "+THAI+" is: ");
    JOptionPane.showMessageDialog(null,result+ " BAHT");
}if (currency == "EURO"){
        JOptionPane.showMessageDialog(null,"The Amount of "+x+ " dollars to be converted to "+EURO+" is: ");
        JOptionPane.showMessageDialog(null,result1+ " EURO");
}if (currency == "JPY"){
            JOptionPane.showMessageDialog(null,"The Amount of "+x+ " dollars to be converted to "+JPY+" is: ");
            JOptionPane.showMessageDialog(null,result2+ " JPY");
}//end THAI if


}//end main
EN

回答 2

Stack Overflow用户

发布于 2015-01-23 12:07:25

要比较字符串,必须使用compareTo()或equals(),它们是由Java定义的。

currency.equals("THAI")currency.compareTo("THAI")==0是一样的

票数 0
EN

Stack Overflow用户

发布于 2015-01-23 12:08:21

你应该使用equals方法比较两个strings...because ==运算符总是比较两个对象是否相同,因为equals方法比较每个字符...

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28102840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档