在我的程序中,我将把用户输入存储在一个数组中,然后检查每个字符以查看它是否是数字、点、E 或负号,然后将其存储到名为 temps
的数组中。
现在我的 float 方法 () 遇到问题,我应该如何为 float 数字-数字-点-数字-数字的模式制定条件(例如 12.22)
我在这里有我的工作:
public void sorting(String data) {
String[] temps = new String[200];
int cpos = 0;
int tpos = 0;
Arrays.fill(temps, null);
if (str.isEmpty() == false) {
char char1 = str.charAt(cpos);
int i = 0;
while (i < str.length()) {
char1 = str.charAt(cpos);
char1 = str.charAt(tpos);
System.out.println("the current value is " + char1 + " ");
tpos++;
if (Character.isDigit(char1)) {
temps[cpos] = "Digit";
// System.out.println(" this number is digit");
cpos++;
} else if (char1 == 'e' || char1 == 'E') {
temps[cpos] = "s_notaion";
cpos++;
} else if (char1 == '-') {
temps[cpos] = "negative";
cpos++;
} else if (char1 == '.') {
temps[cpos] = ".";
cpos++;
}
i++;
}
}
}
这里是 float 的方法
private static boolean floating(String [] data) {
int count =0;
boolean correct = false;
for (int i = 0; i < data.length; i++) {
if (data[i]== "Digit" )
&& data[i]=="." && data[i]"Digit"){
// here is the problem for the condition
}
}
return false;
}
最佳答案
如果我理解正确的话,数据数组有类似 ["Digit","Digit",".","Digit"] 的内容
所以你想要
私有(private)静态 boolean float (String [] data){
如果数组只有“Digit”条目并且恰好有一个“.”,则方法返回 true入口?是这样吗?
如果是这样:
boolean foundLeDigit = false;
for (int i = 0; i < data.length; i++) {
if (data[i].equals("Digit") == false && data[i].equals(".") == false {
//we found something other than a Digit or . it's not a float
return false;
}
if(data[i].equals(".")) {
if(foundLeDigit) { return false; //as we found 2 "." }
foundLeDigit = true
}
}
return foundLeDigit;
关于java - 如何检查字符串是否为 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35109636/