0

ドライバー クラス内の複数のメソッドを使用して、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

4

1 に答える 1

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.
    }
}
于 2012-11-16T03:22:58.240 に答える