我试图在main()中调用scanDouble()方法,但是几次失败

import java.util.Scanner;

public class validateInput {
    public static void scanDouble(Scanner in){
        System.out.print("Enter a number: ");
        if(!in.hasNextDouble()){
            String word = in.next();
            System.err.println(word + "is not a number");
            return;
        }
        double x = in.nextDouble();
        printLogarithm(x);
    }

    public static void printLogarithm(double x) {
        if (x < 0.0) {
            System.err.println("Error: x must be positive.");
            return;
        }
        double result = Math.log(x);
        System.out.println("The log of x is " + result);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double x = in.nextDouble();
        scanDouble(x);
    }

}



  错误信息:validateInput.java:26:错误:不兼容类型:双精度无法转换为扫描仪
  scanDouble(x);

最佳答案

public static void scanDouble(Scanner in){


您必须传递一个Scanner作为参数。

double x = in.nextDouble();
        scanDouble(x);


您将双精度值传递给了方法

10-06 05:30