Java 输入对话框的货币转换器If语句

Java 输入对话框的货币转换器If语句,java,Java,我的项目是创建一个货币转换器,而我似乎无法将if语句连接到用户的选择 import javax.swing.JOptionPane; public class Test { public static void main(String args[]){ double CanadianDollar = .9813; double Euro = .757; double IndianRupee = 52.53; double JapaneseYen

我的项目是创建一个货币转换器,而我似乎无法将if语句连接到用户的选择

import javax.swing.JOptionPane;

public class Test {



    public static void main(String args[]){

    double CanadianDollar = .9813;
    double Euro = .757;
    double IndianRupee = 52.53;
    double JapaneseYen = 80.92;
    double MexicanPeso = 13.1544;
    double BritishPound = .6178;

    String input; 
    Object[] possibilities = {"Canadian Dollar", "Euro", "Indian Rupee", "Japanese Yen",
            "Mexican Peso", "British Pound"};
    String currencyChosen = (String) JOptionPane.showInputDialog( null,
         "What currency would you like to start with?:",
         "Currency Converter",
         JOptionPane.QUESTION_MESSAGE,
         null,
         possibilities,
         null);

    JOptionPane.showMessageDialog(null, "You chose to start from " + currencyChosen);

    Object[] possibilitiesToChoose = {"Canadian Dollar", "Euro", "Indian Rupee", "Japanese Yen",
            "Mexican Peso", "British Pound"};
    String convertTo = (String) JOptionPane.showInputDialog( null,
         "What currency would you like to convert to?:",
         "Currency Converter",
         JOptionPane.QUESTION_MESSAGE,
         null,
         possibilities,
         null);

    JOptionPane.showMessageDialog(null, "You chose to convert from " + currencyChosen + " to " + convertTo);


    if (currencyChosen = "Canadian Dollar")   <- This is not working

    }
}
import javax.swing.JOptionPane;
公开课考试{
公共静态void main(字符串参数[]){
双倍加拿大元=0.9813;
双欧元=0.757;
双印度卢比=52.53;
双日本眼=80.92;
双倍墨西哥比索=13.1544;
双英镑=0.6178;
字符串输入;
对象[]可能性={“加元”、“欧元”、“印度卢比”、“日元”,
“墨西哥比索”、“英镑”};
String CurrencySelected=(String)JOptionPane.showInputDialog(null,
“您希望以什么货币开始?:”,
“货币转换器”,
JOptionPane.QUESTION\u消息,
无效的
可能性,
无效);
showMessageDialog(null,“您选择从”+currencySelected开始);
对象[]可能性选择={“加元”、“欧元”、“印度卢比”、“日元”,
“墨西哥比索”、“英镑”};
String convertTo=(String)JOptionPane.showInputDialog(null,
“您想兑换成什么货币?:”,
“货币转换器”,
JOptionPane.QUESTION\u消息,
无效的
可能性,
无效);
showMessageDialog(null,“您选择从“+CurrencySelected+”转换为“+convertTo”);
如果(CurrencySelected=“加拿大元”)尝试替换此项:

 if (currencyChosen = "Canadian Dollar") 
使用
equals
方法:

  if ("Canadian Dollar".equals(currencyChosen )) 
   ...

用于赋值的
=
不用于检查字符串之间的相等性。

在Java中比较字符串时,需要使用equals方法

if(string1.equals(string2))
因为string1和string2可能具有相同的值,但没有引用相同的对象

if(string1 == string2)
仅当string1和string2实际引用同一对象时才会返回true

你可以在报纸上读到


(编辑:您的代码实际上将“加拿大元”的值指定给CurrencySelected。请注意“=”和“=”)之间的差异

谢谢您的帮助。但是,当我运行程序时,它不会经过“如果”criteria.post thr错误您有吗?我编辑了它以便更容易理解。对此很抱歉。我是编程新手,所以我只是想学习。