即使输入有下划线,此代码也会返回false。 输入将类似于1_3 / 4 + 3并将返回4_3 / 4
String[] Separated = fraction.split(" "); //Splits the tokens up
String firstToken = Separated[0]; // I created this to try to troubleshoot
boolean Mixed = true; //This would determine how much I will need to split up
for(int i = 0; i < firstToken.length(); i++) {
if(firstToken.charAt(i) == '_') {
Mixed = true;
}
else {
Mixed = false;
}
}
答案 0 :(得分:1)
您可以使用&#34;包含&#34;而不是循环:
boolean mixed = firstToken.contains("_");
请在java中使用小写变量名。
答案 1 :(得分:0)
如果您想获得下划线字符的位置,我们的代码可以快速修复:
String firstToken = "_"; // I created this to try to troubleshoot
boolean Mixed = true; //This would determine how much I will need to split up
int i;
for(i = 0; i < firstToken.length(); i++) {
if(firstToken.charAt(i) == '_') {
Mixed = true;
break;
}
else {
Mixed = false;
}
}
//i gives the index of underscore
虽然有更简洁的方法可以做到这一点。 这应该得到下划线的索引
String firstToken = Seperated[0]; // I created this to try to troubleshoot
int index==firstToken.indexOf('_');