gpt4 book ai didi

java.util.InputMismatchException 通过读取 double

转载 作者:太空宇宙 更新时间:2023-11-04 15:21:07 24 4
gpt4 key购买 nike

当我尝试读取 0.6 时,我得到了 java.util.InputMismatchException 这里是代码的一部分。正如您所看到的,我尝试重新实现练习表的 SkipList。

public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double f = scan.nextDouble();
impl_with_errors list = new impl_with_errors(f);
int n = scan.nextInt();


public class impl_with_errors {
public static double chance;
public Node list0;
public Node list1;
public Node list2;
public Node list3;
/**
* the constructor of the skiplist
* @param p the chance that an element shall be in a higher list
*/
public impl_with_errors(double p) {
chance = p;
list0 = null;
list1 = null;
list2 = null;
list3 = null;
}

最佳答案

来自文档:

public double nextDouble()

Scans the next token of the input as a double. This method will throw InputMismatchException if the next token cannot be translated into a valid double value. If the translation is successful, the scanner advances past the input that matched.

解析 0.6 应该可以,但如果你想输入 0,6 你可以使用这个:

String dstr = scan.nextLine();

dstr = dstr.replace(",", ".");

double f = Double.parseDouble(dstr);

关于java.util.InputMismatchException 通过读取 double,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20372814/

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