答案家

 找回密码
 立即注册
查看: 1128|回复: 0

java程序实现整钱兑零源码答案

[复制链接]

1万

主题

1万

帖子

81万

积分

校长

Rank: 9Rank: 9Rank: 9

积分
817232
发表于 2016-12-24 15:32:12 | 显示全部楼层 |阅读模式

package nameyu;import java.util.Scanner;public class Test {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input=new Scanner(System.in);
        System.out.print("Enter an amount in double,for example 11.56:");
        double amount=input.nextDouble();
        int remainingAmount=(int)(amount*100);
        int numberOfOneDollars=remainingAmount/100;
        remainingAmount=remainingAmount%100;
        int numberOfQuarters=remainingAmount/25;
        remainingAmount=remainingAmount%25;
        int numberOfDimes=remainingAmount/10;
        remainingAmount=remainingAmount%10;
        int numberOfNickels=remainingAmount/5;
        remainingAmount=remainingAmount%5;
        int numberOfPennies=remainingAmount;
        String output="Your amount"+amount+"consists of\n";
        if (numberOfOneDollars>1){
            output+="\t"+numberOfOneDollars+"dollars\n";
        }else if(numberOfOneDollars==1){
            output+="\t"+numberOfOneDollars+"dollar\n";
        }else
            output=output;
        if (numberOfQuarters>1){
            output+="\t"+numberOfQuarters+"quarters\n";
        }else if(numberOfQuarters==1){
            output+="\t"+numberOfQuarters+"quarter\n";
        }else
            output=output;
        if (numberOfDimes>1){
            output+="\t"+numberOfDimes+"dimes\n";
        }else if(numberOfDimes==1){
            output+="\t"+numberOfDimes+"dime\n";
        }else
            output=output;
        if (numberOfNickels>1){
            output+="\t"+numberOfNickels+"nickels\n";
        }else if(numberOfNickels==1){
            output+="\t"+numberOfNickels+"nickel\n";
        }else
            output=output;
        if (numberOfPennies>1){
            output+="\t"+numberOfPennies+"pennies\n";
        }else if(numberOfPennies==1){
            output+="\t"+numberOfPennies+"pennie\n";
        }else
            output=output;
        System.out.println(output);
    }
    }   

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

CopyRight(c)2016 www.daanjia.com All Rights Reserved. 本站部份资源由网友发布上传提供,如果侵犯了您的版权,请来信告知,我们将在5个工作日内处理。
快速回复 返回顶部 返回列表