Java驱动程序类,Runnable和main方法

时间:2012-11-16 03:12:08

标签: java methods driver main runnable

我将使用驱动程序类中的多个方法在Java中创建程序。以前,我们只在这些应用程序中使用了main方法。

我知道我会使用这样的东西:

public static void main(String[] args)
{
    U4A4 u = new U4A4();
    u.run();
}

运行方法public U4A4()

是的,我知道这是非常基本的,但我整个晚上一直在寻找,我认为这里的某个人可以用简单的语言来说明我应该怎么做。

当我尝试将public class U4A4 implements Runnable放在我的代码顶部时(我的导入之后),我的编译器生气了,并开始希望我把它抽象化。我不知道那是什么。

那么,我在哪里放implements Runnable以及我在哪里使用run()

非常感谢你在这里与我联系。

编辑:这是我到目前为止所得到的。 http://pastebin.com/J8jzzBvQ

1 个答案:

答案 0 :(得分:3)

您已实现Runnable接口,但未覆盖该接口的run方法。我已经注释了代码,您必须放置线程逻辑,以便线程适合您。

import java.util.Scanner;

public class U4A4 implements Runnable
{
    private int count = 0;
    private double accum = 0;
    private int apr, min, months;
    private double balance, profit;

    public static void main(String[] args)
    {
        U4A4 u = new U4A4();
        u.run();
    }

    public U4A4()
    {
        Scanner in = new Scanner(System.in);

        System.out.print("Enter credit card balance: ");
        balance = in.nextDouble();
        System.out.print("\n\nEnter minimum payment (as % of balance): ");
        min = in.nextInt();
        System.out.print("\n\nEnter annual percentage rate: ");
        apr = in.nextInt();

        profit = this.getMonths(balance);

        System.out.println("\n\n\n# of months to pay off debt =  " + count);
        System.out.println("\nProfit for credit card company = " + profit + "\n");
    }

    public double getMonths(double bal)
    {
        double newBal, payment;
        count++;

        payment = bal * min;

        if (payment < 20 && bal > 20)
        {
            newBal = bal * (1 + apr / 12 - 20);
            accum += 20;

        } else if (payment < 20 && bal < 20)
        {
            newBal = 0;
            accum += bal;
        } else
        {
            newBal = bal * (1 + apr / 12) - payment;
            accum += payment;
        }
        if (newBal != 0) {
            getMonths(newBal);
        }

        return accum;
    }

    public void run() {
        // TODO Auto-generated method stub
        // You have to override the run method and implement main login of your thread here.
    }
}