gpt4 book ai didi

java - 为什么我的实时结果没有在 main 中更新?

转载 作者:行者123 更新时间:2023-12-02 10:00:20 26 4
gpt4 key购买 nike

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

如果我在它自己的 main 方法中运行“bitcoinlive”类,它会正确运行,但当我创建文件的实例时,它不会工作。我需要它来打印比特币的实时值(value)。

我尝试打印出变量“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");

}

这是我单独的类(class)

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);

关于java - 为什么我的实时结果没有在 main 中更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55681408/

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