for 循环(记住最后查看的位置)&字符.isDigit()
我必须做一个关于元素周期表的项目。我的困境如下。用户向您发送一个分子方程。它可以是任意长度。该数组中有大写字母、小写字母和数字。
我还有一系列对象。每个对象代表元素周期表上的一个元素。现在我需要将用户发送给我的字符串分成更小的部分 - 对象数组可以识别的部分。我还必须将答案乘以 1 或元素最后一个字母旁边的数字。
我已经尝试过以下操作。使字符串成为字符数组。从向后运行数组 - 测试数字 (Character.isDigit(a[i]);
),测试大写和小写......我总是遇到同样的问题。我不知道这根绳子有多长。假设我找到了一个号码。然后,如果前一个字符是小写字母,我需要再次向后检查大写字母。然后假设我有分子方程和需要乘以它的量 - 如何让计算机知道从最后一个大写字母开始查找。
我希望有人能理解这一点!
确实需要一些帮助。
另一个问题:为什么这段代码不起作用:
String moleq = "HeKiLH2B6";
char[] a = moleq.toCharArray();
int g = moleq.length()-1;
int x = 1; //if not more than one of element - multiply getWeight by 1
int[][] indexofdigit = new int[moleq.length()][2];
int[] indexoflower = new int[moleq.length()];
for (int i = g; i <= 0; i--){
if (Character.isDigit(a[i])) {
String z = Character.toString(a[i]);
x = Integer.parseInt(z);
System.out.println("YES");
}
}
这段代码永远不会向我打印“是”?
I have to do a project about the periodic table of elements. My dilemma is as follow. The user send you a molecular equation. It can be of any length. In this array there is Uppercase letters, Lowercase letters and numbers.
I also have an array of objects. Each object represents a element on the periodic table. Now I need to break this string the user send me up in smaller pieces - in pieces that is recognized by the array of objects. I also have to multiply the answer by either one or by the number right next to the last letter of the element.
I already tried the following. Make the string a char array. Run through the array from backwards - test for number (Character.isDigit(a[i]);
), test for uppercase and lowercase... I always end up with the same problem. I have no idea how long the string will be. Lets say I find a number. Then if the previous char is an lowercase letter I need to do another check backwards for the uppercase letter. Then let's say I have the molecular equation and the amount I need to multiply it by - how do I let the computer know to start looking from the last uppercase letter.
I hope someone understand this!
Really in need of some help.
Another question: why come doesn't this code work:
String moleq = "HeKiLH2B6";
char[] a = moleq.toCharArray();
int g = moleq.length()-1;
int x = 1; //if not more than one of element - multiply getWeight by 1
int[][] indexofdigit = new int[moleq.length()][2];
int[] indexoflower = new int[moleq.length()];
for (int i = g; i <= 0; i--){
if (Character.isDigit(a[i])) {
String z = Character.toString(a[i]);
x = Integer.parseInt(z);
System.out.println("YES");
}
}
This code never print me a Yes?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您寻找 i <= 0 条件时,您的循环永远不会执行。起初这不是真的,因为 g>0,如果你改变为 i>= 0 它将起作用
Your loop never executes as you are looking for i <= 0 condition. And it is not true at first, as g>0, if you change as i >= 0 it will work
对于化学公式,也许正则表达式(regexp)会很有趣。类似于 ([AZ][az]?[0-9])+。如果我没记错的话,这会隔离一个元素及其基数(例如 Fe2O3 --> 第 1 组 (Fe2) + 第 2 组 (O3))。查看 Pattern 和 Matcher 来了解这一点。
For chemical formulas, maybe it would be interesting a regular expression (regexp). Something like ([A-Z][a-z]?[0-9])+. If I am not wrong() , this isolates an element and its cardinality (for example Fe2O3 --> group 1 (Fe2) + group 2 (O3)). Look at Pattern and Matcher for this.
请注意,使用 Java 5+,您可以使用 foreach 循环使该循环更容易:
要扩展 SJuan 的建议:
产量:
编辑:如果用户输入类似
He-Ki-L-H2-B6 的内容,这也将起作用
或He Ki L H2 B6
。Note that with Java 5+ you can make that loop easier, using the for each loop:
To expand SJuan's suggestion:
Yields:
Edit: this would also work if the user entered something like
He-Ki-L-H2-B6
orHe Ki L H2 B6
.代码中 SJuan76 的答案(添加了不情愿的组限定符):
这打印:
The answer of SJuan76 in code (with the reluctant group qualifier added):
This prints: