更改计算器程序java(循环)
编写一个程序,重复读取0到0之间的整数 100表示美分的数字。把这个数字换算成 美分等于四分之一硬币、一角硬币、五分镍币和 便士。程序应输出最大数量的 适合的25美分硬币,然后是适合的10美分硬币的最大数量 剩下什么,等等。然后程序会要求下一个 数量如果金额为负数,则程序应退出 这就是我到目前为止所拥有的,我不知道如何使它循环或计算数字或每枚硬币更改计算器程序java(循环),java,loops,calculator,Java,Loops,Calculator,编写一个程序,重复读取0到0之间的整数 100表示美分的数字。把这个数字换算成 美分等于四分之一硬币、一角硬币、五分镍币和 便士。程序应输出最大数量的 适合的25美分硬币,然后是适合的10美分硬币的最大数量 剩下什么,等等。然后程序会要求下一个 数量如果金额为负数,则程序应退出 这就是我到目前为止所拥有的,我不知道如何使它循环或计算数字或每枚硬币 System.out.println("Enter number of cents (Negative value to quit):"); int
System.out.println("Enter number of cents (Negative value to quit):");
int cents;
cents = scan.nextInt();
while (cents > 0 )
{
if (cents >= 25)
{
System.out.println("Quarter");
cents -= 25;
}
else if ( cents >= 10 )
{
System.out.println("Dime");
cents -= 10;
}
else if (cents >= 5 )
{
System.out.println("Nickle");
cents -= 5 ;
}
else if (cents >= 1 )
{
System.out.println("Penny");
cents -= 1;
}
}
您可以将问题分解为两部分:
在金额为非负数时询问金额do
- 用整数除法分解给定数量
Scanner scan = new Scanner(System.in);
int input;
do {
input = scan.nextInt();
decompose(input);
} while (input > 0);
2) 用另一种方法编写分解:
public static void decompose(int cents) {
if(cents >= 25) {
int quot = cents / 25;
System.out.println(quot + " Quarter");
cents -= quot * 25;
}
if(cents >= 10) {
int quot = cents / 10;
System.out.println(quot + " Dime");
cents -= quot * 10;
}
[...]
}
您可以将问题分解为两部分:
在金额为非负数时询问金额do
- 用整数除法分解给定数量
Scanner scan = new Scanner(System.in);
int input;
do {
input = scan.nextInt();
decompose(input);
} while (input > 0);
2) 用另一种方法编写分解:
public static void decompose(int cents) {
if(cents >= 25) {
int quot = cents / 25;
System.out.println(quot + " Quarter");
cents -= quot * 25;
}
if(cents >= 10) {
int quot = cents / 10;
System.out.println(quot + " Dime");
cents -= quot * 10;
}
[...]
}
我的建议是使用HashMap。您的代码可以是这样的:
System.out.println("Enter number of cents (Negative value to quit):");
Map<String, Long> countMap = HashMap<String, Long>();
countMap.put("Quarter", 0);
countMap.put("Dime", 0);
countMap.put("Nickle", 0);
countMap.put("Penny", 0);
int cents;
cents = scan.nextInt();
while (cents > 0 )
{
if (cents >= 25)
{
System.out.println("Quarter");
countMap.put("Quarter", countMap.get("Quarter")+1L);
cents -= 25;
}
else if ( cents >= 10 )
{
System.out.println("Dime");
countMap.put("Dime", countMap.get("Dime")+1L);
cents -= 10;
}
else if (cents >= 5 )
{
System.out.println("Nickle");
countMap.put("Nickle", countMap.get("Nickle")+1L);
cents -= 5 ;
}
else if (cents >= 1 )
{
System.out.println("Penny");
countMap.put("Penny", countMap.get("Penny")+1L);
cents -= 1;
}
}
我的建议是使用HashMap。您的代码可以是这样的:
System.out.println("Enter number of cents (Negative value to quit):");
Map<String, Long> countMap = HashMap<String, Long>();
countMap.put("Quarter", 0);
countMap.put("Dime", 0);
countMap.put("Nickle", 0);
countMap.put("Penny", 0);
int cents;
cents = scan.nextInt();
while (cents > 0 )
{
if (cents >= 25)
{
System.out.println("Quarter");
countMap.put("Quarter", countMap.get("Quarter")+1L);
cents -= 25;
}
else if ( cents >= 10 )
{
System.out.println("Dime");
countMap.put("Dime", countMap.get("Dime")+1L);
cents -= 10;
}
else if (cents >= 5 )
{
System.out.println("Nickle");
countMap.put("Nickle", countMap.get("Nickle")+1L);
cents -= 5 ;
}
else if (cents >= 1 )
{
System.out.println("Penny");
countMap.put("Penny", countMap.get("Penny")+1L);
cents -= 1;
}
}
您还可以使用
%
(模)进行计算。通过这种方式,您可以在根本不循环的情况下解决任务。您还可以使用%
(模)进行计算。这样你就可以不用循环来解决这个任务了。为什么不直接使用4个int
变量呢?比哈希映射简单多了。为什么不使用4个int
变量呢?比散列映射简单得多。