货币兑换商

时间:2019-10-22 11:21:10

标签: java

我正在尝试编写将在AED,MYR,USD之间交换的代码。我找到了以下代码,但无法修复该错误。

看来system.in尚未关闭,所以我写了inclose()。但是我仍然得到相同的结果。

我的问题可能是其他原因,而我却没有看到它。

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

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,因此我将其作为主要单位,然后转换为所需的单位。 编辑

     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),但是主变量class不是0,也不会转换为AED。

所以我明白了:谢谢。转换后的量为0.0 1000.0。我们将收取50佣金,您将获得0.0

2 个答案:

答案 0 :(得分:0)

您应该致电scanner.nextLine() (in.nextLine())而不是in.toString()

您还可以使用同一台扫描仪,而不能创建3种不同的扫描仪。

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

    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()

     

将此扫描程序前进到当前行之后,并返回输入   被跳过了。此方法返回当前行的其余部分,   不包括结尾处的任何行分隔符。位置设置为   下一行的开始。

     

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

以上内容将解决您的代码未读取任何输入的问题。

稍后您将无法比较输入的String用户:

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

提示:首选使用“ String” .equals(variable)来避免变量为null时出现空指针异常。

答案 1 :(得分:0)

字符串比较错误。

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