gpt4 book ai didi

java - 为什么我的 Scanner 变量没有使用 : System. out.println(input.nextDouble()); 转换为 double 值?

转载 作者:行者123 更新时间:2023-12-01 23:28:37 25 4
gpt4 key购买 nike

好吧,我似乎无法通过乘以 inputP * inputR 来找到我的兴趣,假设这是因为即使使用此方法,我的扫描仪变量 inputR 和 inputP 仍然没有转换为双变量: System.out.println(inputR .nextDouble()); - 问题是什么?

import java.util.Scanner;

public class test {


//This program will display the value of the principle for each of the next 5 years

public static void main(String[] args) {

Scanner inputR = new Scanner(System.in); Scanner inputP = new Scanner(System.in);
double years = 0;

System.out.println("Please enter the principle value for year one: ");

System.out.println(inputP.nextDouble());


System.out.println("Please enter the interest rate for year one: ");

System.out.println(inputR.nextDouble());

while (years < 5) {

double interest;
years = years + 1;

interest = inputP * inputR;

principle = inputP + interest;

System.out.println("Your principle after 5 years is: " + principle);

}
}
}

最佳答案

Scanner 变量无法“转换为double”。对于 Java 专家来说,即使有这样的想法也是陌生的。您可能有 JavaScript 等动态语言的背景,这个概念至少有一定的意义。

实际上发生的情况是 nextDouble 方法返回一个 double,并且您必须将该值捕获到 double 中 变量,或内联使用它。

另一点:您不得在同一输入流上使用两个扫描仪。仅使用一个并根据需要多次调用其 nextDouble 方法,每次都会检索从输入流解析的下一个 double 值。

关于java - 为什么我的 Scanner 变量没有使用 : System. out.println(input.nextDouble()); 转换为 double 值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19644534/

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