Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在字符列表中计算两位数_Java_Groovy - Fatal编程技术网

Java 在字符列表中计算两位数

Java 在字符列表中计算两位数,java,groovy,Java,Groovy,我正在写一个groovy程序。我的输出如下 红色5绿色5蓝色10白色15 我想把输出中的数字加起来。总数是35 我写了下面的逻辑,但是程序返回17,这是正确的,因为下面的逻辑考虑了数字 你能告诉我如何让程序理解两位数吗?这样我才能得到正确的总数 for (int i =0; i < output.length(); i++) { { sum=sum+Character.getNumericValue(grps.charAt(i))

我正在写一个groovy程序。我的输出如下

红色5绿色5蓝色10白色15

我想把输出中的数字加起来。总数是35

我写了下面的逻辑,但是程序返回17,这是正确的,因为下面的逻辑考虑了数字

你能告诉我如何让程序理解两位数吗?这样我才能得到正确的总数

for (int i =0; i < output.length(); i++)
    {
        {
            sum=sum+Character.getNumericValue(grps.charAt(i))
        }
    }
for(int i=0;i
感谢

我将使用正则表达式用空格替换所有非数字,
trim()
删除任何前导空格、尾随空格和空格,然后在(可选连续)空格上拆分;像

String output = "red 5green 5blue 10white 15";
int sum = 0;
for (String token : output.replaceAll("\\D+", " ").trim().split("\\s+")) {
    sum += Integer.parseInt(token);
}
System.out.println(sum);
产出(按要求)

另一个选项是
模式
,找到一个或多个数字的所有序列并对它们进行分组,然后使用循环解析并添加到
总和
。像

Pattern p = Pattern.compile("(\\d+)");
Matcher m = p.matcher(output);
while (m.find()) {
    sum += Integer.parseInt(m.group(1));
}
System.out.println(sum);

这也将为您提供
35

显然,我不能在注释中编写代码,但为了继续回答,您可以进一步使用斜杠字符串,删除分号和System.out

String output = "red 5green 5blue 10white 15"
int sum = 0

for (String token : output.replaceAll(/\D+/, " ").trim().split(/\s+/)) {
    sum += Integer.parseInt(token)
}

println(sum)

由于您也标记了[groovy]:

您可以使用regexp
/\d+/
对字符串中的字符串进行“findAll”编号,将所有字符串都转换为数字,最后对它们进行
sum()
运算。e、 g

def sumNumberStrings(s) {
    s.findAll(/\d+/)*.toLong().sum()
}
assert 35==sumNumberStrings("red 5green 5blue 10white 15")

不需要使用正则表达式或其他复杂功能的简单算法如下:

String output = "red 5green 5blue 10white 15";

// The result
int sum = 0;
// Sum of the current number
int thisSum = 0;
for (char c : output.toCharArray()) {
    // Iterate through characters
    if (Character.isDigit(c)) {
        // The current character is a digit so update thisSum so it includes the next digit.
        int digitValue = c - '0';
        thisSum = thisSum * 10 + digitValue;
    } else {
        // This character is not a digit, so add the last number and set thisSum 
        // to zero to prepare for the next number.
        sum += thisSum;
        thisSum = 0;
    }
}
// If the string ends with a number, ensure it is included in the sum.
sum += thisSum;

System.out.println(sum);

这适用于任意位数的数字。

我唯一想添加的是,可以为正则表达式使用斜杠字符串,并且可以删除分号和System.out字符串输出=“red 5green 5blue 10white 15”int sum=0 for(字符串标记:output.replaceAll(/\D+/,”).trim().split(/\s+/){sum+=Integer.parseInt(token)}println(sum)太棒了。谢谢ElliottNice one。谢谢AliceThanks@cfrick。现在又遇到了一个问题。我的输出有以下以空格分隔的数字,这些数字需要相乘。[120.98 7151.99 8141.39 4137.71 7121.27 6187.29 11]尝试使用split和multiply将它们按空格分隔,但是面临问题,有什么想法吗?谢谢,现在还有一个问题。我的输出有以下按空格分隔的数字,这些数字需要乘以。[120.98 7、151.99 8、141.39 4、137.71 7、121.27 6、187.29 11]试着用空间分割和繁殖它们,但是面对问题,有什么想法吗?
String output = "red 5green 5blue 10white 15";

// The result
int sum = 0;
// Sum of the current number
int thisSum = 0;
for (char c : output.toCharArray()) {
    // Iterate through characters
    if (Character.isDigit(c)) {
        // The current character is a digit so update thisSum so it includes the next digit.
        int digitValue = c - '0';
        thisSum = thisSum * 10 + digitValue;
    } else {
        // This character is not a digit, so add the last number and set thisSum 
        // to zero to prepare for the next number.
        sum += thisSum;
        thisSum = 0;
    }
}
// If the string ends with a number, ensure it is included in the sum.
sum += thisSum;

System.out.println(sum);