【问题标题】:how can i add values from a loop in java如何从java中的循环中添加值
【发布时间】:2013-08-22 00:16:23
【问题描述】:

我最近收到一个关于 uni 的问题,涉及信用卡声明,上面写着我有一串数字,然后我将这些数字转换为单独的整数,然后根据它们的 10 的幂增加它们使用 horners 方法在字符串中的位置 然后我必须添加我从循环中获得的值以生成 1 个整数。 我知道这是将字符串转换为 int 的一种奇怪方法,但我的作业指出我必须使用 horners 方法来转换字符串,而不是使用内置的 java 类/方法

我的问题是,如何添加单独的加权数字并将它们连接成一个数字。

如果有帮助的话,可以举个例子,

给定一个卡号 1234,该数字根据其位置和长度加权,因此:

1 - 1000
2 - 200
3 - 30
4 - 4

然后将这些相加以创建一个整数

1, 2, 3,4 ---> 1234

这是我目前的代码

public static long toInt(String digitString) {
    long answer = 0;
    long val = 0;
    String s = "";
    for (int j = 0; j < digitString.length(); j++) {
        val = digitString.charAt(j) - '0';
        val = (long) (val * Math.pow(10, (digitString.length() - 1) - j));
        System.out.println(val);

    }
    return answer;
}

【问题讨论】:

  • 什么没有按预期工作?如果你喂它 1234 会发生什么?

标签: java loops numbers concatenation


【解决方案1】:

我认为您的代码不是面向对象的,而且很难阅读和理解。
基本,问题是映射,非常简单。
如果您使用 Java 编写代码,最好以 OO 方式使用,尽管我不太喜欢 Java。 查看我的代码

@Test
public void testCardScoreSystem() {
    Map<String, String> scoreMapping = new HashMap<String, String>();
    scoreMapping.put("1", "1000");
    scoreMapping.put("2", "200");
    scoreMapping.put("3", "30");
    scoreMapping.put("4", "4");

    String[] input = {"1", "2", "3", "4"};

    long score = 0;
    for (String str : input) {
        String mappedValue = scoreMapping.get(str);

        if (mappedValue == null) {
            throw new RuntimeException("Hey dude, there is no such score mapping system! " + str);
        }

        score += Long.valueOf(mappedValue);
    }

    System.out.println(score);
}

【讨论】:

    【解决方案2】:

    很可能我没有关注你,因为这听起来太简单了。 但要返回一个长整数(或整数),您只需将这些数字相加即可:

    public static long toLong(String digitString) {
        long answer = 0;
        long val = 0;
        for (int j = 0; j < digitString.length(); j++) {
            val = digitString.charAt(j) - '0';
            val = (long) (val * Math.pow(10, (digitString.length() - 1) - j));
            answer += val; // here! :)
            //System.out.println(val);
        }
        return answer;
    }
    

    请注意,这不适用于负数,所以这里有一个更复杂的版本:

    public static long toLong(String digitString) {
        long answer = 0;
        long val = 0;
        boolean negative = false;
        int j = 0;
    
        if (digitString.charAt(0) == '-') {
            negative = true;
            j = 1;
        } else if (digitString.charAt(0) == '+')
            j = 1;
    
        for (; j < digitString.length(); j++) {
            if (!Character.isDigit(digitString.charAt(j)))
                throw new NumberFormatException(digitString);
    
            val = digitString.charAt(j) - '0';
            val = (long) (val * Math.pow(10, (digitString.length() - 1) - j));
            answer += val;
        }
    
        return negative ? -answer : answer;
    }
    

    此代码适用于负数以及以 + 号开头的奇怪数字。如果有其他字符,会抛出异常。

    【讨论】:

      猜你喜欢
      • 2017-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 2011-07-08
      相关资源
      最近更新 更多