java - 如何执行这个java双扫描仪?
问题描述
当调用 scanDouble(); 我得到一个错误。
package com.company;
import java.util.Random;
import java.util.Scanner;
public class Main {
Scanner in = new Scanner(System.in);
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 scanDouble(Scanner in) {
System.out.print("Enter the name: ");
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 main(String[] args) {
scanDouble();
}
}
有谁知道如何执行 scanDouble(); 我应该在方法调用中包含一些内容吗?
解决方案
如果我遵循您的逻辑,请尝试in
在scanDouble()
方法中传递 Scanner 变量,如下所示:
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
scanDouble(in);
}
例如,将双精度值x
作为参数传递是行不通的,我只是对其进行了测试。
推荐阅读
- javascript - 使用 Jasmine 测试链式承诺而不返回承诺
- django - 无法使用 DRF 在 Django 模板中显示图像
- wpf - 在本地/直接设置控件的(触发)属性时,ControlTemplate/Style 触发器不起作用
- javascript - Vue.js - 在另一个数组中显示嵌套数组的详细信息
- c - 如何反转C中的句子?
- paypal - 什么是 Paypal 的定期付款配置文件 RPSTATE 参数?
- postgresql - 更改 client_min_messages 和 log_min_messages
- python-3.x - 您如何比较包含不可散列对象的字典?
- c - 字符串计数问题、浮点整数、基于字母、单词和句子数量的评分公式,在 C 中
- jquery - 选中复选框时选择特定的单选选项jQuery