为什么我的实时结果没有在 main 中更新?
Why isn't my live result updating in main?
我正在制作一个货币追踪器来追踪比特币的实时价值。
class "bitcoinlive" 运行s 如果我 运行 它在它自己的主要方法中是正确的,但是当我创建一个实例时它不会工作文件。我需要它来打印比特币的实时价值。
我尝试打印出变量 "a53" 但我不知道我做的是否正确。
这是 bitcoinlive class 的导入列表,因为它一直给我一条错误消息,并且在发布时不允许将其作为代码的一部分。
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;
}
}
您的 class Bitcoinlive
将价格存储在名为 a53
的字段中。您可以通过调用 get()
来更新此字段。但是,您似乎从未调用过 get()
- 您只是调用了字段:
System.out.println(mybitcoinlive.a53);
尝试将该行替换为:
System.out.println(mybitcoinlive.get());
或者先刷新一下:
mybitcoinlive.get();
System.out.println(mybitcoinlive.a53);
我正在制作一个货币追踪器来追踪比特币的实时价值。
class "bitcoinlive" 运行s 如果我 运行 它在它自己的主要方法中是正确的,但是当我创建一个实例时它不会工作文件。我需要它来打印比特币的实时价值。
我尝试打印出变量 "a53" 但我不知道我做的是否正确。 这是 bitcoinlive class 的导入列表,因为它一直给我一条错误消息,并且在发布时不允许将其作为代码的一部分。
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;
}
}
您的 class Bitcoinlive
将价格存储在名为 a53
的字段中。您可以通过调用 get()
来更新此字段。但是,您似乎从未调用过 get()
- 您只是调用了字段:
System.out.println(mybitcoinlive.a53);
尝试将该行替换为:
System.out.println(mybitcoinlive.get());
或者先刷新一下:
mybitcoinlive.get();
System.out.println(mybitcoinlive.a53);