for 循环(记住最后查看的位置)&字符.isDigit()

发布于 2024-11-01 17:11:05 字数 931 浏览 1 评论 0原文

我必须做一个关于元素周期表的项目。我的困境如下。用户向您发送一个分子方程。它可以是任意长度。该数组中有大写字母、小写字母和数字。

我还有一系列对象。每个对象代表元素周期表上的一个元素。现在我需要将用户发送给我的字符串分成更小的部分 - 对象数组可以识别的部分。我还必须将答案乘以 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江湖彼岸 2024-11-08 17:11:06

当您寻找 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

ま昔日黯然 2024-11-08 17:11:06

对于化学公式,也许正则表达式(regexp)会很有趣。类似于 ([AZ][az]?[0-9])+。如果我没记错的话,这会隔离一个元素及其基数(例如 Fe2O3 --> 第 1 组 (Fe2) + 第 2 组 (O3))。查看 Pattern 和 Matcher 来了解这一点。

  • 如果我错了,我确信谷歌的某个地方有一个公式的正则表达式。它通常不依赖于语言(如果它适用于 Perl,它也适用于 Java)。

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.

  • If I am wrong, I am sure there is a regexp for formulas somewhere in Google. It usually is not language dependent (if it works for Perl it works for Java).
池木 2024-11-08 17:11:06

请注意,使用 Java 5+,您可以使用 foreach 循环使该循环更容易:

for (char c : a){
  if (Character.isDigit(c)) {
    String z = Character.toString(c);
    x = Integer.parseInt(z);
    System.out.println("YES");
  }
}

要扩展 SJuan 的建议:

String input = "HeKiLH2B6";
Pattern p = Pattern.compile("([A-Z][a-z]*)(\\d*)");    
Matcher m = p.matcher( input );

while(m.find()){
  String element = m.group( 1 );
  String cardinalityStr = m.group( 2 );
  int cardinality= 1;
  if( cardinalityStr != null && cardinalityStr .length() > 0)
  {
    cardinality= Integer.parseInt( cardinalityStr );
  }

  System.out.println( element + cardinality);
}

产量:

He1
Ki1
L1
H2
B6

编辑:如果用户输入类似 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:

for (char c : a){
  if (Character.isDigit(c)) {
    String z = Character.toString(c);
    x = Integer.parseInt(z);
    System.out.println("YES");
  }
}

To expand SJuan's suggestion:

String input = "HeKiLH2B6";
Pattern p = Pattern.compile("([A-Z][a-z]*)(\\d*)");    
Matcher m = p.matcher( input );

while(m.find()){
  String element = m.group( 1 );
  String cardinalityStr = m.group( 2 );
  int cardinality= 1;
  if( cardinalityStr != null && cardinalityStr .length() > 0)
  {
    cardinality= Integer.parseInt( cardinalityStr );
  }

  System.out.println( element + cardinality);
}

Yields:

He1
Ki1
L1
H2
B6

Edit: this would also work if the user entered something like He-Ki-L-H2-B6 or He Ki L H2 B6.

青巷忧颜 2024-11-08 17:11:06

代码中 SJuan76 的答案(添加了不情愿的组限定符):

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {
        // the compiled pattern can be re-used, are thread-safe 
        // and thus can be static final
        Pattern p = Pattern.compile("([A-Z][a-z]?[0-9]*)+?");

        // Per molecule (well, string) a matcher must be obtained.
        Matcher m = p.matcher("HeKiLH2B6");
        while(m.find()) {
            System.out.println(m.group());
        }
    }
}

这打印:

He
Ki
L
H2
B6

The answer of SJuan76 in code (with the reluctant group qualifier added):

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {

    public static void main(String[] args) {
        // the compiled pattern can be re-used, are thread-safe 
        // and thus can be static final
        Pattern p = Pattern.compile("([A-Z][a-z]?[0-9]*)+?");

        // Per molecule (well, string) a matcher must be obtained.
        Matcher m = p.matcher("HeKiLH2B6");
        while(m.find()) {
            System.out.println(m.group());
        }
    }
}

This prints:

He
Ki
L
H2
B6
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文