-3
我的任务需要我读取用户输入的号码并输出号码所生成的硬币。例如,如果用户输入“37”,程序应该回应(1季度,1毛钱和2便士)。我最有可能的代码根本没有任何意义,我不知道我需要做什么来解决它。需要帮助,使我的第一个Java代码。 (Coin Change)
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;
}
}
}
如何实现这一点。 (我是新的,认真不明白这一点) –
我很惊讶任务会要求你在没有太多语言理解的情况下做到这一点 - 每当你使用getQuarters(美分)时,它会返回有多少宿舍可以“适合“这些美分。例如,'System.out.println(getQuarters(100))'输出“4”。我会更新答案,只需等待一分钟 – MCMastery
因此,按照您刚才的方式进行操作,那就是代码的数学方面?剩下的只是输出? –