类似于ATM的Java程序

时间:2018-09-25 18:46:30

标签: java

我正在尝试制作一个类似于ATM的Java程序。当我从ATM取款时,Java程序需要告诉我要取多少张钞票,例如,如果我 2430返还应该是100张中的24张和10张中的3张,而只有ATM 在10张中取钱20 50 100欧元纸币。我该怎么办?

它看起来像这样:

 public class Withdraw
{
  public static void main(String[] args) throws Exception
  {
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    int n=Integer.parseInt(br.readLine());
    if(n>15000)
    {
      System.out.println("ATM Cash Limit exceeds.");
    }
    else
    {
      if(n<500)
      {
        System.out.println(n/100+" Hundreds");
      }
      else
      {
        int h=5;
        int f=(n-500)/500;
        //System.out.println(n-500+" "+(n-500)/500+" "+(n-500)%500);
        h += ((n-500)%500)/100;
        if(h>5)
        {
          f=f+1;
          h=h-5;
        }
        System.out.println(f+" Five Hundreds and "+h+" Hundreds");
      }
    }
  }
}

3 个答案:

答案 0 :(得分:1)

以下代码必须为您完成任务,包括在500,100,50,20,10,1中提取资金。

public class Withdraw
{
 public static void main(String[] args)
 {

      int moneyValue=2430;
      int[] noteValues= {500,100,50,20,10,1};
      if(moneyValue>15000)
      {
          System.out.println("ATM Cash Limit exceeds.");
      }
      else
      {
         for(int i=0;i<noteValues.length && moneyValue!=0;i++)
         {
             if(moneyValue>=noteValues[i])
                 System.out.println("No of "+noteValues[i]+"'s"+" :"+moneyValue/noteValues[i]);
             moneyValue=moneyValue%noteValues[i];
         }
      }
  }
}   



 Output:
  No of 500's :4
  No of 100's :4
  No of 20's :1
  No of 10's :1

答案 1 :(得分:0)

一种方法是从最大音符开始向后工作:

//    n = 2430

int f = 0;
while (n >= 500) {
    f++;
    n -= 500;
}

int h= 0;
while (n >= 100) {
    h++;
    n -= 100;
}

// other values

System.out.println(f + " Five Hundreds and "+ h +" Hundreds" + ... );

此外,考虑使用命名良好的变量。例如,将其命名为f,而不是countOfFiveHundredEuroBankNotes

另外,提取常量,例如:

private static final int FIVE_HUNDRED_EUROS = 500;

这有助于使代码更具可读性:

int remainingRequestedAmount = n;

while (remainingRequestedAmount >= FIVE_HUNDRED_EUROS) {
    countOfFiveHundredEuroBankNotes++;
    remainingRequestedAmount -= FIVE_HUNDRED_EUROS;
}

答案 2 :(得分:0)

您可以先从最大的账单金额开始,然后将提取的钱除以。如果该数字大于0,则将其四舍五入。那会给你数量的。然后,您可以取出数量*比拉芒。

与下一个相同(只要金额是从最大到最小的顺序)

E.G:

public class Withdraw
{
    public static void main(String[] args) throws Exception
    {
        int moneyWithdrawn = 2040;
        final int[] amountsFromLargestToSmallest = { 100, 50, 20, 10};
        final int smallestBillAmount = 10;

        int[] amountsOfEachBill = new int[amountsFromLargestToSmallest.length];

        if (moneyWithdrawn > 15000)
        {
            System.out.println("ATM Cash Limit exceeds.");
        }
        // Must be divisible by 10, since that's our smallest bill
        else if (moneyWithdrawn <= 0 || moneyWithdrawn % 10 != 0)
        {
            System.out.println("Please enter a valid number.");
        }
        else
        {
            for (int i = 0; i < amountsFromLargestToSmallest.length; i++)
            {
                int billAmount = amountsFromLargestToSmallest[i];

                int amount = (int) Math.floor(moneyWithdrawn / billAmount);

                if (amount > 0)
                {
                    moneyWithdrawn -= amount * billAmount;
                }

                amountsOfEachBill[i] = amount;
            }
        }
    }
}