简体   繁体   中英

Simple Java Query - Getting a Result from a String

I have been trying to learn Java for the past few days so my my knowledge is incredibly basic.

I cannot for the life of me work out how to search for the answer to my question online.

I am trying to create a very simple currency converter. The user inputs a currency symbol, then using an IF statement I want to specify the value of 'currencyamnt'

I want to essenially

if ( currency = "£"  )

    {
        currencyamnt = 1;
    }

currency being a string and currencyamnt being an float.

This doesnt work and i am not sure why..

In java you have to use the .equals to check if a string is equal to some other string. In something like c++ this is valid but for java instead of doing this:

if ( currency == "£"  ) {
    currencyamnt = 1;
}

try

if ( currency.equals("£")) {
    currencyamnt = 1;
}

Edit: The original code looked like this:

if ( currency = "£"  ) {
    currencyamnt = 1;
}

Which would have set currency to £ instead of checking whether it was equivalent text which is something to always look out for when coding!

currency.equals(“£”)应该这样做。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM