本文介绍了需要帮助制作我的第一个Java代码。 (硬币变化)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的作业要求我读取用户输入的数字并输出数字所产生的硬币。例如,如果用户输入37,则程序应响应(1 1/4,1和1便士)。我最有可能的代码根本没有任何意义,我不知道我需要做些什么来修复它。
My assignment requires me to read a number entered by the user and output the coins that number makes. For example, if the user enters "37", the program should respond with (1 Quarter, 1 dime, and 2 pennies). The code I have most likely does not make any sense at all and I don't know what I need to do to fix it.
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;
}
}
}
推荐答案
划分和下限(你应该重新开始)
Divide and floor (you should probably start over)
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方法
Remember to include the getQuarters method in your Java class
示例:
-
getQuarters(25) - > ; 1
-
getQuarters(24) - > 0
-
getQuarters(49) - > 1
-
getQuarters(51) - > 2
getQuarters(25) -> 1
getQuarters(24) -> 0
getQuarters(49) -> 1
getQuarters(51) -> 2
这篇关于需要帮助制作我的第一个Java代码。 (硬币变化)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!