更改计算器未显示更改

时间:2014-11-18 18:47:56

标签: java class

这是我第一次参加java课程并且为了我的作业,我想写一个Class和Driver来显示返回的变化以及硬币。 即输入商品价格:45.50 - >输入现金:50.00 - >变化是4.50 - >美元:4&宿舍:2

这是班级。

public class Change 
{
    private int price;
    private int cash;

public Change (int p, int c)  { price =p; cash =c; }

public Change() {
}

public int getPrice ( ) { return price;} 

public void setPrice (int p) {price=p;}

public int getCash ( ) { return cash;}

public void setCash (int c) {cash=c;}

    public int computeChange( ) {return cash-price; }
    public int toDollars ( ) {return computeChange() / 100; }
    public int aChanges ( ) {return computeChange() % 100; }
    public int toQuarters ( ) {return aChanges() / 25; }
    public int bChanges ( ) {return aChanges() % 25; }
    public int toDimes ( ) {return bChanges() / 10; }
    public int cChanges ( ) {return bChanges() % 10; }
    public int toNickels ( ) {return cChanges() / 5; }  
    public int dChanges ( ) {return cChanges() % 5; }   
    public int toPennies ( ) {return dChanges() / 1; }
    public int eChanges (  ) {return dChanges() % 1; }      
   }    

这是我的驱动程序

import java.util.Scanner;
public class TestChange{

   public static void main (String[] args)
   {

    double cash;
    double price;
    double changes = 0;
    int change;
    Scanner scan = new Scanner (System.in);

   Change c = new Change ();

   System.out.println ("Enter the price of the item: ");
   price = scan.nextDouble();

   System.out.println ("Enter cash amount: ");
   cash = scan.nextDouble();
   int cents = (int) (changes * 100);

   changes = c.computeChange();

   System.out.println ("Change: " + changes );

System.out.println ("Dollar bills is: " + c.toDollars());
System.out.println ("Quarters is: " + c.toQuarters());
System.out.println ("Dimes is: " + c.toDimes());
System.out.println ("Nickels is: " + c.toNickels());
System.out.println ("Pennies is: " + c.toPennies());
   }
}

2 个答案:

答案 0 :(得分:0)

您的问题是,在创建price对象时,您未使用cashChange。您需要使用Change对象的构造函数,您可以在其中指定以美分为单位的价格,以及以美分为单位的现金。

删除行

Change c = new Change ();

并替换

int cents = (int)(changes * 100);

使用新行

Change c = new Change ((int)(price * 100 + 0.5), (int)(cash * 100 + 0.5));

这样,您就可以从用户输入的数字中创建Change对象;并将所有内容乘以100以将其变为数分。额外的0.5是因为如果您使用double变量存储一定金额,有时会出现舍入错误,因为double变量通常不会准确存储十进制数字 - 根据你如何使用它们,将它们用于赚钱可能是一个坏主意。

答案 1 :(得分:0)

我相信这会奏效。

更改摘要 -

  1. Cange类型的价格和现金从“改变”类中的int加倍并相应地更改getter,setter和构造函数
  2. 使用参数化构造函数创建“更改”类的对象。
  3. 您可能必须相应地更改类“Change”的方法体,因为成员变量的类型从int更改为double。但我想你明白了。

    import java.text.DecimalFormat;
    import java.util.Scanner;
    
    public class TestChange{
    
    public static void main (String[] args)
    {
    double cash;
    double price;
    double changes = 0;
    DecimalFormat df = new DecimalFormat("#.##");
    
    Scanner scan = new Scanner (System.in);
    
    System.out.println ("Enter the price of the item: ");
    price = scan.nextDouble();
    
    System.out.println ("Enter cash amount: ");
    cash = scan.nextDouble();
    
    Change c = new Change (price,cash);
    changes = c.computeChange();
    
    System.out.println ("Change: " + df.format(changes) );
    System.out.println ("Dollar bills is: " + c.toDollars());
    System.out.println ("Quarters is: " + c.toQuarters());
    System.out.println ("Dimes is: " + c.toDimes());
    System.out.println ("Nickels is: " + c.toNickels());
    System.out.println ("Pennies is: " + c.toPennies());
    }
    }
    
    
    class Change 
    {
    private double price;
    private double cash;
    
    public Change (double p, double c)  
    { 
    this.price =p; 
    this.cash =c; 
    }
    
    public Change() 
    {
    }
    
    public double getPrice ( ) { return this.price;} 
    public void setPrice (double p) {this.price=p;}
    public double getCash ( ) { return cash;}
    public void setCash (double c) {this.cash=c;}
    
    public double computeChange( ) {return cash-price; }
    public int intcomputeChange( ) {return (int) (cash*100-price*100); }
    public int toDollars ( ) {return (int)computeChange(); }
    public int aChanges ( ) {return  intcomputeChange() % 100; }
    public int toQuarters ( ) {return aChanges() / 25; }
    public int bChanges ( ) {return aChanges() % 25; }
    public int toDimes ( ) {return bChanges() / 10; }
    public int cChanges ( ) {return bChanges() % 10; }
    public int toNickels ( ) {return cChanges() / 5; }  
    public int dChanges ( ) {return cChanges() % 5; }   
    public int toPennies ( ) {return dChanges() / 1; }
    public int eChanges (  ) {return dChanges() % 1; }      
    }