首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >货币兑换商

货币兑换商
EN

Stack Overflow用户
提问于 2019-10-22 11:21:10
回答 2查看 89关注 0票数 0

我试图写一个代码,将交换之间的AED,MYR,美元。我得到了下面的代码,我无法修复错误。

看起来system.in没有关闭,所以我写了inclose()。但我还是得到了同样的结果。

我的问题可能是别的什么,但我看不见。

编辑:这是当前更改的代码。

代码语言:javascript
复制
import java.util.Scanner;

public class CurrencyConverter
{
     protected static long amount;
     static long Namount ;
     static int commesion;

     static String to;

    public static void main( String[] args )
    {
    CurrencyConverterM MSg=new CurrencyConverterM();
    CurrencyConverterM account1 = new CurrencyConverterM( );
     String from ;
    for(int i=0 ;i<3; i++)
     {
        System.out.println("Please enter your currency (USD, AED, or MYR only): ");
        Scanner in = new Scanner( System.in );
        from = in.nextLine();
        System.out.println("What currency do you want?: ");
        String to = in.nextLine();
        System.out.println("How much you want to convert?: ");
        amount= in.nextLong();
                //in.close();

    if ("USD".equals(from)) {
        amount=((long) (amount*0.27));
        amount=account1.converter(to, amount);
    }
    else if ("MYR".equals(from)) {
        amount=((long) (amount * 1.14));
        amount =account1.converter(to, amount);
    }
    else {
        if(mmount >= 900) {
            Namount = (amount-50);
            commesion =50;

        }
        else 
        {
            Namount = (amount - 10);
            commesion = 10;
        }
    }
        System.out.println(MSg.getMsg());

}

    }

}

输出应如下。询问当前货币:询问你想要什么货币:询问金额。我正在将任何金额转换为AED,所以我将其作为主要单元,然后转换为所希望的单元。编辑

代码语言:javascript
复制
     public class CurrencyConverterM extends CurrencyConverter
     {
        long am;
     @SuppressWarnings("static-access")
   long converter (String to,long amount)
   {

    if ("MYR".equals(to)) {
        am=(super.amount*=0.88);

    }
    else if ("MYR".equals(to)) {
        am=(super.amount*=3.7);

    }
    return am ;
}



@SuppressWarnings("static-access")
public  String getMsg()
{
    return ("Thank you. The converted amount is "+(super.amount) + ". We will take" +super.commesion + " commission, and you will get "+ super.Namount);
}

}

以前它没有读取用户的输入,现在它没有转换值。每次计算后,我尝试打印出我的值,但是看起来变量am没有被正确计算,它被乘以0或除以1(最后的结果总是0),但是主类中的值不是0,它也没有转换为AED。

所以我明白了:谢谢。折算金额为0.0 1000.0。我们将委托take50,你将得到0.0

EN

回答 2

Stack Overflow用户

发布于 2019-10-22 11:29:42

您应该调用scanner.nextLine() (in.nextLine())而不是in.toString()

此外,您也可以使用相同的扫描仪,而不是创建3个不同的扫描仪。

请参阅使用一个扫描器(In)更新的代码的以下部分:

代码语言:javascript
复制
    System.out.println("Please enter your currency (USD, AED, or MYR only): ");
    Scanner in = new Scanner( System.in );
    from = in.nextLine();
    System.out.println("What currency do you want?: ");
    String to = in.nextLine();
    System.out.println("How much you want to convert?: ");
    amount= in.nextLong();
    System.out.println(from + " " + to);
    in.close();

通过执行in.nextLine(),可以读取下一行用户输入。

来自java 8 javadocs:

nextLine

公共字符串nextLine()

将此扫描器移过当前行,并返回跳过的输入。此方法返回当前行的其余部分,不包括末尾的任何行分隔符。该位置设置为下一行的开头。

由于此方法继续在输入中搜索寻找行分隔符,因此如果不存在行分隔符,则可以缓冲所有对行的搜索以跳过。

以上将解决代码不读取输入的问题。

稍后,您将遇到比较输入的字符串用户的问题:

from=="USD"应改为"USD".equals(from)

提示:当变量为null时,更愿意使用“String”.equals(变量)来避免空指针异常。

票数 0
EN

Stack Overflow用户

发布于 2019-10-22 11:35:31

字符串比较是错误的。

from=="MYR"应该是from.equals("MYR"),而我建议不区分大小写的equalsIgnoreCase

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

https://stackoverflow.com/questions/58503139

复制
相关文章

相似问题

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