需要帮助制作我的第一个 Java 代码.(硬币兑换)



我的作业要求我读取用户输入的数字并输出该数字产生的硬币。例如,如果用户输入"37",程序应响应(1 季度、1 角钱和 2 便士)。我拥有的代码很可能根本没有任何意义,我不知道我需要做什么来修复它。

import java.util.Scanner;
public class Change
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner (System.in);
        Int n = sc.nextInt("Enter a positive integer" );
        int number1, number2; // Division operands
        int quotient;         // Result of division

            if (QtrCnt > 0)
                if (QtrCnt > 1)
                    System.out.println(QtrCnt + " quarters");
                else
                    System.out.println(QtrCnt + " quarter");
        }
        if (DimeCnt > 0)
        {
            if (DimeCnt > 1)
                System.out.println(DimeCnt + " dimes");
            else
                System.out.println(DimeCnt + " dime");
        }
        if (NicklCnt > 0)
        {
            if (NicklCnt > 1)
                System.out.println(NicklCnt + " nickles");
            else
                System.out.println(NicklCnt + " nickle");
        }
        if (PennyCnt > 0);
        {
            if (PennyCnt > 1);
                System.out.println(PennyCnt + " pennies");
            System.out.println(PennyCnt + " penny");
        }
        int q = 25;
        int d = 10;
        int n = 5;
        int p = 1;
        if (a < 0);
            System.out.println("ERROR");

            String (money >=25); { int numQuarters = money/ 25; }
            money -= numQuarters * 25;
            QtrCnt = (num1 - num1 % 25) / 25;
            num1 = num1 - QtrCnt * 25;
            String(money >=10); { int numDimes = money/ 10; }
            money -= numDimes * 10;
            DimeCnt = (num1 - num1 % 10) / 10;
            num1 = num1 - DimeCnt * 10;
            String (money >=5); { int numNickles = money/ 5; }
            money -= numNickles * 5;
            NicklCnt = (num1 - num1 % 5) / 5;
            num1 = num1 - NicklCnt * 5;
            String (money >=1); { int numPennies = money/ 1; }
            money -= numPennies * 1;
            PennyCnt = (num1 - num1 % 1) / 1;
            num1 = num1 - PennyCnt * 1;
        }
   }
 }

分隔和地板(你可能应该重新开始)

public static int getQuarters(int cents) {
    return Math.floor(cents / 25.0);
}

以下是您的操作方法:

public static void main(String[] args) {
    int cents = 46; // 46 just for an example
    int left = cents; // a variable that represents how many cents are left
    int quarters = getQuarters(cents); // how many quarters fit into 46 (1)
    int left -= quarters * 25; // now we have 21 cents left we need to work out
    int dimes = getDimes(left); // you can implement getDimes yourself (look at getQuarters). This will now return 2, since 2 dimes can go into 21 cents.
    left -= dimes * 10; // we now have only 1 cent to account for
    int nickels = getNickels(left) // Returns 0 (no nickels can fit into 1 cent)
    left -= nickels * 5; // we still have 1 cent left
    int pennies = left; // how many pennies are left over (always < 5)
    System.out.println(cents + " cents = " + quarters + " Quarters, " + dimes + " Dimes, " + nickels + " Nickels, and " + pennies + " Pennies."); // print the output 
}

请记住在Java类中包含getQuarters方法

例子:

  • getQuarters(25) -> 1
  • getQuarters(24) -> 0
  • getQuarters(49) -> 1
  • getQuarters(51) -> 2

相关内容

最新更新