| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 705 人关注过本帖
标题:(求助)Java, 拜托各位帮我看一下这个怎样做
只看楼主 加入收藏
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
结帖率:25%
收藏
已结贴  问题点数:10 回复次数:6 
(求助)Java, 拜托各位帮我看一下这个怎样做
我用的是 JCreator, 是我电脑课的作业,后天要交了,拜托大家帮帮忙, 就是要写一个会自动找零钱的程序,5块钱买2.35快糖,剩下的钱要用硬币分类列出来没多少个2快,多少个1快,多少个25分,多少个10分,多少个5分,多少个1分,用的硬币越少越好, 题目是有点长,希望帮帮忙

(1) The Change Calculator

You are off to the candy store with a $5 bill to buy some candies. Write a program called
ChangeCalculatorProgram that allows you to enter the total cost of the candies that you are
buying (i.e., use the Scanner class) in cents (i.e., 275 cents is $2.75). The program should
then tell you how many toonies, loonies, quarters, dimes, nickels and pennies that you should
get as your change (assuming that you always pay with a $5 bill).

Your program should return the least amount of coins possible. All your calculations
should be in terms of cents so that you can use just int variables.

Your program should have nice-looking output as follows:

Enter the cost of the candies (in cents): 235
The change from $5.00 for $2.35 of candies is:
1 toonie(s)
0 loonie(s)
2 quarter(s)
1 dime(s)
1 nickel(s)
0 pennie(s)

Notice that amount entered is an integer (i.e., total cents) but the output statement displays the
cost as a float (i.e., dollars). Test your code with the following costs:
0, 25, 100, 101, 107, 150, 235, 400, 498, 499, 500


需要解释的加我qq 452323960, 下面是我写的code, 可以找到要找多少钱,但是我不知道怎样才可以分类列出来


import java.util.Scanner;

class ChangeCalculatorProgram {
    public static void main(String args[]) {
        int m,c,change;
        Scanner keyboard = new Scanner(System.in);
        
        System.out.println("Enter the cost of the candies (in cents):");
        m = keyboard.nextInt();
        c = keyboard.nextInt();
   
        change = m-c;//compute the change and store it
        System.out.println("Given numbers "+m+
                           "and"+c+
                               ":");
        System.out.println("The change from $5.00 for $2.35 of candies is"+change);
        System.out.println("The cost is "+c);
        
        
    }
}


[ 本帖最后由 lyf3368 于 2009-10-6 12:23 编辑 ]
搜索更多相关主题的帖子: Java 
2009-10-06 12:08
多多关照
Rank: 2
等 级:论坛游民
帖 子:42
专家分:51
注 册:2009-8-22
收藏
得分:3 
这个题目很简单呀。
提示:至于硬币分类,你可以用除法和求余运算来完成。
试着做一下吧,如果真不行的话,我把我写的给你
                                                                                 
2009-10-06 16:34
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
以下是引用多多关照在2009-10-6 16:34:34的发言:

这个题目很简单呀。
提示:至于硬币分类,你可以用除法和求余运算来完成。
试着做一下吧,如果真不行的话,我把我写的给你
                                                                                 
我试过很多方法了,不知道为什么不行,和想要的结果总是有出入,我刚学编程不久,而且明天要交了,你写的给我看一下可以吗
2009-10-06 20:21
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
哈哈,我终于想出来了

import java.util.Scanner;
 
class ChangeCalculatorProgram {
    public static void main(String args[]) {
        int money,cost,change,toonies,loonies,quarters,dimes,nickels,pennies;
        Scanner keyboard = new Scanner(System.in);
        money = 500;
        System.out.println("Enter the cost of the candies (in cents):");
        cost = keyboard.nextInt();
        toonies = (500-cost)/200; //compute the change in toonies and store it
        loonies = ((500-cost)-(toonies*200))/100; //compute the change in loonies and store it
        quarters = ((500-cost)-(toonies*200)-(loonies*100))/25; //compute the change in quarters and store it
        dimes = ((500-cost)-(toonies*200)-(loonies*100)-(quarters*25))/10; //compute the change in dimes and store it
        nickels = ((500-cost)-(toonies*200)-(loonies*100)-(quarters*25)-(dimes*10))/5; //compute the change in nickels and store it
        pennies = ((500-cost)-(toonies*200)-(loonies*100)-(quarters*25)-(dimes*10)-(nickels*5))/1; //compute the change in pennies and store it     
         
        System.out.println("The change from $5.00 for $2.35 of candies is:");
        System.out.println(toonies+ "toonie(s)");     
        System.out.println(loonies+ "loonie(s)");         
        System.out.println(quarters+ "quarter(s)");     
        System.out.println(dimes+ "dime(s)");
        System.out.println(nickels+ "nickel(s)");
        System.out.println(pennies+ "pennie(s)");
    }
}

这是我的做法,希望大家可以帮我看看另外2道题,我完全不会做  在这个帖子 https://bbs.bccn.net/thread-287725-1-1.html  感激不尽



     
        

[ 本帖最后由 lyf3368 于 2009-10-7 06:12 编辑 ]
2009-10-07 06:10
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
收藏
得分:3 
以下是引用lyf3368在2009-10-7 06:10:58的发言:

哈哈,我终于想出来了

import java.util.Scanner;
 
class ChangeCalculatorProgram {
    public static void main(String args[]) {
        int money,cost,change,toonies,loonies,quarters,dimes,nickels,p ...
你的程序重用性很低,这题用面向对象思想来做比较好些

你的优秀和我的人生无关!!!!
    
    我要过的,是属于我自己的生活~~~
2009-10-07 07:58
lyf3368
Rank: 1
等 级:新手上路
帖 子:26
专家分:0
注 册:2009-10-6
收藏
得分:0 
以下是引用lampeter123在2009-10-7 07:58:27的发言:

你的程序重用性很低,这题用面向对象思想来做比较好些
我新手一个就想到这种烂方法,你可以帮我看一下第3和第4题吗,我完全不会做,  拜托帮我一下,不然我死定了

https://bbs.bccn.net/thread-287725-1-1.html

[ 本帖最后由 lyf3368 于 2009-10-7 08:34 编辑 ]
2009-10-07 08:19
gameohyes
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:湖南
等 级:版主
威 望:53
帖 子:1275
专家分:3629
注 册:2009-3-5
收藏
得分:3 
先去了解下面向对象吧。
      很不错的。
代码多写的话,就会有点头绪了。

C#超级群 74862681,欢迎大家的到来!
2009-10-08 23:45
快速回复:(求助)Java, 拜托各位帮我看一下这个怎样做
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018736 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved