我正在制作一个货币跟踪器来跟踪比特币的实时价值。

如果以自己的main方法运行“ bitcoinlive”类,则可以正确运行,但是在创建文件实例时它将无法工作。我需要它来打印您的比特币的实时价值。

我尝试打印出变量“ a53”,但我不知道我是否做对了。
这是bitcoinlive类的导入列表,因为它一直给我一个错误消息,并且在发布时不会将其与代码分开。

public static void main(String[] args) {
    Dates d = new Dates();
    String s = d.getDate();
    System.out.println("Date is" + s);
    W3 mywallet = new W3();
    Bitcoinlive mybitcoinlive = new Bitcoinlive();
    L3 myledger = new L3();
    Scanner myscanner = new Scanner(System.in);
    double buy = 0.0;
    int choice = 0;
    double bitcoin = 4000;
    double USD = 20000;

    while (choice != 5) {
        System.out.println("Welcome! Enter a command. \n"
                + "Enter 1) Buy Bitcoin \n"
                + "Enter 2) Sell Bitcoin  \n"
                + "Enter 3) Print Balance \n"
                + "Enter 4) Print History \n"
                + "ENTER 5) Exit Program\n");
        choice = myscanner.nextInt();

        if (choice == 1) {
            System.out.println("How many? ");
            buy = myscanner.nextDouble();
            mywallet.add(buy);
            bitcoin = bitcoin * buy;
            USD = USD - bitcoin;

            myledger.save(s);

            System.out.println("you have bought:" + mywallet.numcoins);
            System.out.println(USD);
            System.out.println(mybitcoinlive.a53);
            bitcoin = 4000;

        } else if (choice == 2 && USD >= bitcoin) {
            System.out.println("How many?");

            buy = myscanner.nextDouble();
            mywallet.subtract(buy);
            System.out.println("you have sold:" + mywallet.numcoins);
            USD = USD + bitcoin;
            System.out.println(USD);
            bitcoin = 4000;
            myledger.save(s);

        } else if (choice == 3) {
            System.out.println("Balance:" + mywallet.numcoins);

        } else if (choice == 4) {
            System.out.println("Transaction history:  ");

            System.out.println("you have made" + myledger.getsize() + "transactions"
                    + d.getDate());

        } else if (choice == 5) {
            // exit
            break;

        } else if (choice == 7) {
            System.out.println(mybitcoinlive.price);
        }

    }

    System.out.println("Bye");

}


这是我单独的课

public class Bitcoinlive {



    Double a53=0.0;
    double price;

    Double get() {

        try {
            String urlcoincapeth13 = "https://api.coinmarketcap.com/v1/ticker/bitcoin/";
            Document docblocktradescoincapeth13 = Jsoup.parse(new URL(urlcoincapeth13).openStream(), "UTF-8", "", Parser.xmlParser());
            String a13 = docblocktradescoincapeth13.toString();
            int a23 = a13.indexOf("price_usd") + 13;
            int a33 = a13.indexOf("price_btc") - 4;
            String a43 = a13.substring(a23, a33);
            a53 = Double.parseDouble(a43);
        } catch (Exception e) {
            System.out.println("Error accessing bitcoin values");
        }

        return a53;
    }
}

最佳答案

您的班级Bitcoinlive将价格存储在名为a53的字段中。您可以通过调用get()来更新此字段。但是,看起来您从未调用get()-您只是在调用字段:

System.out.println(mybitcoinlive.a53);


尝试将该行替换为:

System.out.println(mybitcoinlive.get());


或先刷新:

mybitcoinlive.get();
System.out.println(mybitcoinlive.a53);

10-08 12:53