gpt4 book ai didi

java - InputDialogs 的货币转换器 If 语句

转载 作者:行者123 更新时间:2023-11-30 03:04:08 26 4
gpt4 key购买 nike

我的项目是创建一个货币转换器,我似乎无法将 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

}
}

最佳答案

尝试替换这个:

 if (currencyChosen = "Canadian Dollar") 

使用equals方法:

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

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

关于java - InputDialogs 的货币转换器 If 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35213261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com