困惑于如何调用类内的方法

时间:2014-10-20 00:19:06

标签: java

所以我需要从用户那里获得输入,但是我需要它来打印出giveFirstClassStamps数量和givePennyStamps数量,我不知道如何做到这一点。 任何帮助或指示正确的方向将不胜感激。

import java.util.*;
/**
*/
public class StampMachine
{
public static final int FIRST_CLASS_STAMP_PRICE = 44;
private int balance;

/**
  Constructs a stamp machine with a zero balance.
*/
     public StampMachine() 
{ 
   balance = 0; 
}

public static void main( String[ ]  args)
{
       Scanner input = new Scanner(System.in);

      System.out.print("Enter 16-Digit Credit Card Number: ");
      String cardNumber = input.nextLine();
      System.out.print("Enter Month/Year of Expiration Date in MM/YY format: ");
      String expirationDate = input.nextLine();
      System.out.print("Stamp Purchase Amount: ");
      int dollars = input.nextInt();


}

/**
   Adds a given number of dollar bills into this machine.
   @param dollars the number of dollar bills
*/
public void insert(int dollars)
{  
    balance = balance + 100 * dollars;
}

/**
   Dispenses first class stamps for the inserted payment.
   @return the number of first class stamps
*/
public int giveFirstClassStamps()
{
   int firstClassStamps = balance / FIRST_CLASS_STAMP_PRICE;
   balance = balance - firstClassStamps * FIRST_CLASS_STAMP_PRICE;
   return firstClassStamps;
}

/**
   Dispenses penny stamps for the inserted payment.
   @return the number of penny stamps
*/
public int givePennyStamps()
{
  int pennyStamps = balance;
   balance = 0;
   return pennyStamps;      
}

2 个答案:

答案 0 :(得分:1)

您可以创建该类的实例,然后调用方法。获取输入变量后尝试以下操作:

StampMachine sm = new StampMachine();
sm.insert(dollars);

您可以继续使用相同的实例“sm”来调用其他方法。

答案 1 :(得分:0)

主要是在获得dollars

之后
call insert method. Modify method sigantures to pass dollars entered by the user
call giveFirstClassStamps and assign the returned value to a local variable inside main
do the same for givePennyStamps and now print the values

希望这有帮助。