如何调用scanDouble()方法?

时间:2018-12-18 08:14:32

标签: java

我尝试在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:错误:类型不兼容:double无法转换为扫描仪   scanDouble(x);

1 个答案:

答案 0 :(得分:1)

public static void scanDouble(Scanner in){

您必须将Scanner作为参数传递。

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

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