java最后一个小问题

要创建房屋预算,您想找出您的净收入,即您的收入减去您的支出。
编写一个Java方法,该方法采用输入字符串并计算收入减去费用。
收入组成部分用数字表示;而您的支出是以减号“-”开头的数字。
输入字符串可能包含小写字母和大写字母以及其他字符。
请注意,Character.isDigit(char)测试字符是否是字符'0','1',直到,'9'之一。另请回想一下,Integer.parseInt(string)将字符串转换为int。
测试用例:
calcNetIncome("工资15000元奖金2000租金-1000Y") → 16000
calcNetIncome(“25000毛收入,-200水,电:-300”)→24500


import java.util.Scanner;

public class test {
    public static int calcNetIncome(String str) {
        int sum = 0;
        for (int i = 0; i < str.length(); i++) {
            if (Character.isDigit(str.charAt(i))) {
                int flag = 1, temp = 0;
                if (str.charAt(i - 1) == '-') {
                    flag = -1;
                }
                while (Character.isDigit(str.charAt(i))) {
                    temp = temp * 10 + str.charAt(i) - '0';
                    i++;
                }
                i--;
                sum += temp * flag;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNextLine()) {
            String str = sc.next();
            int result = calcNetIncome(str);
            System.out.println(result);
        }
        sc.close();
    }
}
package study.hospital5.com;

import lombok.SneakyThrows;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test {
    @SneakyThrows
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            calcNetIncome(s);
        }
    }

    private static void calcNetIncome(String s) {
        Pattern compile = Pattern.compile("[-]?[0-9]+");
        Matcher matcher = compile.matcher(s);
        List<Integer> data = new ArrayList<>();
        while (matcher.find()) {
            data.add(Integer.parseInt(matcher.group()));
        }
        Integer sum = 0;
        for (Integer item : data) {
            sum += item;
        }
        System.out.println(String.format("calcNetIncome(\"%s\") → %s", s, sum));
    }
}

遍历字符串,将负号和数字字符加入字符串,并转换为数值,最后累加就行了。

import java.util.Scanner;

public class test {
public static int calcNetIncome(String str) {
int sum = 0;
for (int i = 0; i < str.length(); i++) {
if (Character.isDigit(str.charAt(i))) {
int flag = 1, temp = 0;
if (str.charAt(i - 1) == '-') {
flag = -1;
}
while (Character.isDigit(str.charAt(i))) {
temp = temp * 10 + str.charAt(i) - '0';
i++;
}
i--;
sum += temp * flag;
}
}
return sum;
}

public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    while (sc.hasNextLine()) {
        String str = sc.next();
        int result = calcNetIncome(str);
        System.out.println(result);
    }
    sc.close();
}

}


package com.yonyou.iuap.formula.engine.dag;

import org.apache.commons.lang3.StringUtils;


/**
 * @program bPaaS_formula
 * @description:
 * @author: lee
 * @create: 2022/10/19 14:37
 */
public class Main {


    public static Integer getIncome(String incomeString) {
        if (StringUtils.isBlank(incomeString)) {
            return 0;
        }
        int sum = 0;
        for (int i = 0; i < incomeString.length(); i++) {
            //遍历获取所有的数字
            if (Character.isDigit(incomeString.charAt(i))) {
                int flag = 1, temp = 0;
                //扣钱
                if (incomeString.charAt(i - 1) == '-') {
                    flag = -1;
                }
                //挣得
                while (Character.isDigit(incomeString.charAt(i))) {
                    temp = 10 * temp + incomeString.charAt(i) - '0';
                    i++;
                }
                i--;
                sum += temp * flag;
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        String string = "工资15000元奖金2000租金-1000Y";
        //16000
        Integer income = getIncome(string);
        System.out.println(income);
    }


}