java - 为什么 IF 语句不适用于舍入浮点数和双精度数?
问题描述
我正在尝试检查用户是否输入了 3.5 和 7 之间的双精度。我尝试使用实际的双精度和Math.round();
,以及StrictMath.round()
。我也尝试解析作为浮点数输入的字符串,但它没有改变任何东西。这是我使用的基本代码:
import java.util.Scanner;
public class IfStatement {
public static void main(String[] args) {
//create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter first double: ");
double number = input.nextDouble();
if (isDouble(number)==true) {
double x = Double.parseDouble(number);
if (3.5 <= x <= 7) {
System.out.println("good");
} else {
System.out.println("incorrect input");
}
} else {
System.out.println("incorroct input");
}
}
public static booleen isDouble(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
++i;
}
int integerPartSize = 0;
int exponentPartSize = -1;
while (i < length) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
exponentPartSize = 0;
} else {
return false;
}
} else if (exponentPartSize > -1) {
++exponentPartSize;
} else {
++integerPartSize;
}
++i;
}
if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1)
|| exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
return false;
}
return true;
}
}
我也尝试在 if 语句中制作 3.5 3 但没有骰子。我只需要一个松散的解释,而不是一个完整的解决方案。
解决方案
我已经编辑了你的代码。
boolean
拼写isDouble()
接受一个字符串a <= x <= b
Java 中不允许使用链式表达式
还,
if ((str.charAt(0) == '0' && i > 1 && exponentPartSize < 1) || exponentPartSize == 0 || (str.charAt(length - 1) == '.')) {
return false;
}
return true;
可以简化为
return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
&& exponentPartSize != 0 && (str.charAt(length - 1) != '.');
这是完整的代码。
class Test {
public static boolean isDouble(String str) {
if (str == null) {
return false;
}
int length = str.length();
if (length == 0) {
return false;
}
int i = 0;
if (str.charAt(0) == '-') {
if (length == 1) {
return false;
}
++i;
}
int integerPartSize = 0;
int exponentPartSize = -1;
while (i < length) {
char c = str.charAt(i);
if (c < '0' || c > '9') {
if (c == '.' && integerPartSize > 0 && exponentPartSize == -1) {
exponentPartSize = 0;
} else {
return false;
}
} else if (exponentPartSize > -1) {
++exponentPartSize;
} else {
++integerPartSize;
}
++i;
}
return (str.charAt(0) != '0' || i <= 1 || exponentPartSize >= 1)
&& exponentPartSize != 0 && (str.charAt(length - 1) != '.');
}
public static void main(String[] args) {
//create a Scanner
Scanner input = new Scanner(System.in);
System.out.print("Enter first double: ");
String number = input.nextLine();
if (isDouble(number)) {
double x = Double.parseDouble(number);
if (3.5 <= x && x <= 7) {
System.out.println("good");
} else {
System.out.println("incorrect input");
}
} else {
System.out.println("incorroct input");
}
}
}
推荐阅读
- shell - 在脚本中使用 awk gensub 的不同结果
- sql-server - SQL Server:varchar 读取 unicode 问题
- ruby-on-rails - 导入带有关联的 CSV
- python - python folium 地图设置(防止缩放和平移)
- python - Django ORM 和 Flask-Sqlalchemy 在使用过滤器上有什么区别?
- java - 在 IntelliJ 2018 的 Java 代码编辑器中,在语句结束分号之前强制一个空格
- java - Java插入排序:“相同”的变量得到不同的结果
- javascript - Angular 6 神秘的堆栈跟踪:globalZoneAwareCallback
- powershell - 如何创建适用于格式表的 PowerShell 数据结构
- javascript - 如何使用 javascript 过滤各种 mime 文件类型