1

当我尝试阅读 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;
    }
4

3 回答 3

0

您的代码将起作用,只需尝试在 main 方法中修改这些行:

    Scanner scan = new Scanner(System.in);
    System.out.println("Enter the double value:\n");
    double f = scan.nextDouble();
    impl_with_errors list = new impl_with_errors(f);
    System.out.println("Enter the integer value:\n");
    int n = scan.nextInt();

它应该工作。

于 2013-12-04T11:00:41.587 回答
0

从文档:

公共双 nextDouble()

将输入的下一个标记扫描为双精度。如果下一个标记无法转换为有效的双精度值,此方法将抛出 InputMismatchException。如果翻译成功,扫描仪会超过匹配的输入。

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

String dstr = scan.nextLine();

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

double f = Double.parseDouble(dstr);
于 2013-12-04T10:53:19.927 回答
0

使用逗号作为小数分隔符,而不是点。0,6作品

用 a 格式化双精度值DecimalFormat df = new DecimalFormat("#,#");

于 2013-12-04T10:40:41.033 回答