gpt4 book ai didi

java - 对如何调用类内的方法感到困惑

转载 作者:行者123 更新时间:2023-12-01 12:24:36 25 4
gpt4 key购买 nike

所以我需要从用户那里获取输入,我已经做到了,但随后我需要它打印出 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;
}

最佳答案

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

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

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

关于java - 对如何调用类内的方法感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26456884/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com