如何在print语句中调用不同类的方法?

时间:2017-04-15 01:21:58

标签: java

我是java的初学者,我正在尝试构建一些吸烟节省计算器。我试图习惯在java中使用对象,但我不知道如何在main方法的print语句中调用一个不同类的方法。我试过在线寻找答案,但我没找到一个适合我的案例。

吸烟计算

public class SmokingCalc {

 public static void main(String args[]) {

    System.out.println("********SMOKING CALCULATOR********");

    Calc cost = new Calc(getPackets, getWeek, getCost);

    System.out.println("How much is a packet of cigarretes?");

    cost.getCost();

    System.out.println("How many packets do you smoke a week?");

    cost.getWeek();

    System.out.println("You would save " + getWeek + "units in one week." );

    }
}

计算值

    import java.util.Scanner;

    public class Calc {
Scanner scan = new Scanner(System.in);
private int packs;
private int weekly;
private int cost;

public Calc(int getPacks, int getCost, int getWeekly) {

    int input = scan.nextInt();
    int input1 = scan.nextInt();
    int input2 = scan.nextInt();

    packs = input;
    weekly = input1;
    cost = input2;
}
public int getPackets() {

    return packs;
}

public int getWeek() {

    return weekly;
}

public int getCost() {

    return cost;
}

public int fourWeeksCost() {
    int fourWeek;
    fourWeek = 4 * cost;
    return fourWeek;
}

public int twelveWeekCost() {
    int twelveWeek;
    twelveWeek = 12 * cost;
    return twelveWeek;
}

public int sixMonthsCost() {
    int sixMonths;
    sixMonths = 26 * cost;
    return sixMonths;
}

public int yearlyCost() {
    int yearly;
    yearly = 52 * cost;
    return yearly;
}

}

我很感激有关如何做到这一点的任何帮助,提前谢谢。

1 个答案:

答案 0 :(得分:0)

假设这可能是解决您问题的方法。

Calc.Java

public class Calc {
private int packs;
private int costPerPack;
private int costPerWeek;

public Calc(int getPacks, int cost ) {

    packs = getPacks;
    costPerPack = cost;
    costPerWeek = getPacks * cost;
}
public int getPackets() {

    return packs;
}

public int getCostPerPack() {

    return costPerPack;
}

public int getCostPerWeek() {

    return costPerWeek;
}

public int fourWeeksCost() {
    int fourWeek;
    fourWeek = 4 * costPerWeek;
    return fourWeek;
}

public int twelveWeekCost() {
    int twelveWeek;
    twelveWeek = 12 * costPerWeek;
    return twelveWeek;
}

public int sixMonthsCost() {
    int sixMonths;
    sixMonths = 26 * costPerWeek;
    return sixMonths;
}

public int yearlyCost() {
    int yearly;
    yearly = 52 * costPerWeek;
    return yearly;
}

}

SmokingCalc.java

public class SmokingCalc {
    public static void main(String[] args) {


        Scanner scan = new Scanner(System.in);
        int costforpack,packs;

        System.out.println("********SMOKING CALCULATOR********");


        System.out.println("How much is a packet of cigarretes?");

        costforpack = scan.nextInt();

        System.out.println("How many packets do you smoke a week?");

        packs = scan.nextInt();

        Calc cost = new Calc(packs, costforpack );

        System.out.println("You would save " + cost.getCostPerWeek() + "units in one week." );


        }
}

希望这有帮助。