用Java写一个应用程序 5
writeanapplicationthatpromptsforandreadsadoublevaluerepresentingamonetaryamount.Thend...
write an application that prompts for and reads a double value representing a monetary amount.Then determine the fewest number of each bill and coin needed to represent that amount, starting with the hightest( assume that a ten dollar bill is the maximum size needed).
比如输入:47.63
程序输出:4 ten dollar bills
1five dollar bills
2 one dollar bills
2 quarters
1dimes
0 nickles
3 pennies 展开
比如输入:47.63
程序输出:4 ten dollar bills
1five dollar bills
2 one dollar bills
2 quarters
1dimes
0 nickles
3 pennies 展开
展开全部
代码:
package Number;
public class DivideNumber {
public void Divide(double x){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
for (int i = 0;x > 10;i++){
x = x-10;
a = a + 1;
}
for (int i = 0;x >5;i++){
x = x-5;
b = b+1;
}
for (int i = 0;x > 1;i++){
x = x-1;
c = c+1;
}
for (int i = 0;x > 0.25;i++){
x = x-0.25;
d = d+1;
}
for (int i = 0;x > 0.1;i++){
x = x-0.1;
e = e+1;
}
for (int i = 0;x > 0.05;i++){
x = x-0.05;
f = f+1;
}
for (int i = 0;x > 0.01;i++){
x = x-0.01;
g = g+1;
}
System.out.println(a+"ten dollar bills "+b+"five dollar bills "+c+"one dollar bills "+d+"quarters "+e+"dimes "+f+"nickles "+g+"pennies");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
}
public static void main(String[] args) {
DivideNumber dividenumber = new DivideNumber();
dividenumber.Divide(47.63);
}
}
说明:该程序只限两位整数+两位小数。
只需把“dividenumber.Divide(47.63);”中的47.63改为其他要求的数即可。
嘿嘿,这个方法比较牛X哦!
package Number;
public class DivideNumber {
public void Divide(double x){
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
int f = 0;
int g = 0;
for (int i = 0;x > 10;i++){
x = x-10;
a = a + 1;
}
for (int i = 0;x >5;i++){
x = x-5;
b = b+1;
}
for (int i = 0;x > 1;i++){
x = x-1;
c = c+1;
}
for (int i = 0;x > 0.25;i++){
x = x-0.25;
d = d+1;
}
for (int i = 0;x > 0.1;i++){
x = x-0.1;
e = e+1;
}
for (int i = 0;x > 0.05;i++){
x = x-0.05;
f = f+1;
}
for (int i = 0;x > 0.01;i++){
x = x-0.01;
g = g+1;
}
System.out.println(a+"ten dollar bills "+b+"five dollar bills "+c+"one dollar bills "+d+"quarters "+e+"dimes "+f+"nickles "+g+"pennies");
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(f);
System.out.println(g);
}
public static void main(String[] args) {
DivideNumber dividenumber = new DivideNumber();
dividenumber.Divide(47.63);
}
}
说明:该程序只限两位整数+两位小数。
只需把“dividenumber.Divide(47.63);”中的47.63改为其他要求的数即可。
嘿嘿,这个方法比较牛X哦!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.util.Scanner;
/**
Author: Crady
Time: {date}下午9:32:55
**/
public class Change {
public static void main(String []agrs)
{
System.out.println("Please input money(n精确到0.01):");
Scanner console=new Scanner(System.in);
double d=console.nextDouble();
String s[]={"ten dollar bills","five dollar bills","one dollar bills",
"quarters","dimes","nickles","pennies"};
int []y=new int[7];
y[0]=getTenDollars(d);
d-=y[0]*10;
y[1]=getFiveDollars(d);
d-=y[1]*5;
y[2]=getOneDollars(d);
d-=y[2];
y[3]=getQuarters(d);
d-=y[3]*0.25;
y[4]= getDimes(d);
d-=y[4]*0.1;
y[5]=getNickles(d);
d-=y[5]*0.05;
y[6]=getPennies(d);
for(int i=0;i<y.length;i++)
{
System.out.print(y[i]+" ");
System.out.println(s[i]);
}
}
public static int getTenDollars(double n)
{
int cout=(int)n/10;
if(cout<1)
return 0;
return cout;
}
public static int getFiveDollars(double n)
{
int cout=(int)n/5;
if(cout<1)
return 0;
return cout;
}
public static int getOneDollars(double n)
{
int cout=(int)n/1;
if(cout<1)
return 0;
return cout;
}
public static int getQuarters(double n)
{
int cout=(int)(n/0.25);
if(cout<1)
return 0;
return cout;
}
public static int getDimes(double n)
{
int cout=(int)(n/0.1);
if(cout<1)
return 0;
return cout;
}
public static int getNickles(double n)
{
int cout=(int)(n/0.05);
if(cout<1)
return 0;
return cout;
}
public static int getPennies(double n)
{
int cout=(int)(n/0.01);
if(cout<1)
return 0;
return cout;
}
}
该方法比较抽象,适用于精确到0.01,从控制台输入想要兑换的money,非常符合java写软件的抽象性……
/**
Author: Crady
Time: {date}下午9:32:55
**/
public class Change {
public static void main(String []agrs)
{
System.out.println("Please input money(n精确到0.01):");
Scanner console=new Scanner(System.in);
double d=console.nextDouble();
String s[]={"ten dollar bills","five dollar bills","one dollar bills",
"quarters","dimes","nickles","pennies"};
int []y=new int[7];
y[0]=getTenDollars(d);
d-=y[0]*10;
y[1]=getFiveDollars(d);
d-=y[1]*5;
y[2]=getOneDollars(d);
d-=y[2];
y[3]=getQuarters(d);
d-=y[3]*0.25;
y[4]= getDimes(d);
d-=y[4]*0.1;
y[5]=getNickles(d);
d-=y[5]*0.05;
y[6]=getPennies(d);
for(int i=0;i<y.length;i++)
{
System.out.print(y[i]+" ");
System.out.println(s[i]);
}
}
public static int getTenDollars(double n)
{
int cout=(int)n/10;
if(cout<1)
return 0;
return cout;
}
public static int getFiveDollars(double n)
{
int cout=(int)n/5;
if(cout<1)
return 0;
return cout;
}
public static int getOneDollars(double n)
{
int cout=(int)n/1;
if(cout<1)
return 0;
return cout;
}
public static int getQuarters(double n)
{
int cout=(int)(n/0.25);
if(cout<1)
return 0;
return cout;
}
public static int getDimes(double n)
{
int cout=(int)(n/0.1);
if(cout<1)
return 0;
return cout;
}
public static int getNickles(double n)
{
int cout=(int)(n/0.05);
if(cout<1)
return 0;
return cout;
}
public static int getPennies(double n)
{
int cout=(int)(n/0.01);
if(cout<1)
return 0;
return cout;
}
}
该方法比较抽象,适用于精确到0.01,从控制台输入想要兑换的money,非常符合java写软件的抽象性……
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
其实就是把一个金额转换成对应的纸币,并且要纸币的总张数最少。
面值有10美元,5美元,1美元,25美分(quarter),10美分(dime), 5美分(nickle), 1美分(penny)
面值有10美元,5美元,1美元,25美分(quarter),10美分(dime), 5美分(nickle), 1美分(penny)
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Lz,你把美元的各个单位值给下。这个是ACM吧? 英文不好看不懂2 quarters。2季度?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
要找给一个人47.63刀,问你怎么找法,用到的钱币的数目最少,水题
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询