这些数字来自哪里?

时间:2015-04-14 15:22:58

标签: java arrays for-loop

我试图自己学习Java,但我遇到了一个我不太了解的问题。 我试着写一个程序来计算交叉总和。我的想法是将输入的数字拆分为char数组然后简单 遍历数组并计算总和。

这就是我所拥有的:

public static int calcCrossSum (long number){
    int sum = 0;
    char[] charArray = String.valueOf(number).toCharArray();

    for( int i = 0; i < charArray.length; i++){
        sum += charArray[i];
    }
    return sum;
}

如果我用数字123尝试这种方法,我得到150  因此,我不知道为什么。我尝试使用调试器,似乎我的变量test为自己添加了奇怪的数字,即使调试器显示数组的内容似乎是正确的。

如果有人能向我解释我做错了什么以及这些数字来自哪里,我将不胜感激。如果有任何不清楚的地方,我会很乐意尝试添加更多信息。

4 个答案:

答案 0 :(得分:2)

Chars具有与之关联的整数值。你应该这样做:

public static int calcCrossSum (long number){
    int sum = 0;
    char[] charArray = String.valueOf(number).toCharArray();

    for( int i = 0; i < charArray.length; i++){
        sum += Integer.parseInt(("" + charArray[i]));
    }
    return sum;
}

答案 1 :(得分:1)

您正在使用每个字符的unicode值进行求和。 值为&#39; 1&#39; = 49,&#39; 2&#39; = 50,&#39; 3&#39; = 51。

如果您想使用unicode值进行求和并获得每个char的实际值,您可以创建如下函数:

public static int charToInt(char c){
    return (int) c - 48; //48 is the value of '0'
}

并调用函数:

public static int calcCrossSum (long number){
    int sum = 0;
    char[] charArray = String.valueOf(number).toCharArray();

    for( int i = 0; i < charArray.length; i++){
        sum += charToInt(charArray[i]);
    }
    return sum;
}

您可以查找每个值here

的unicode值

答案 2 :(得分:1)

问题在于您要添加字符,但必须添加整数。因为+上的char操作数将始终为您提供其unicodes值的总和。

而是找到你可以做的数字总和

public static int calcCrossSum (long number){
    int sum = 0;
    while ( number > 0 )
        {
            sum += number % 10; 
            number /= 10; 

        } 
    return sum;
}

Demo

答案 3 :(得分:0)

这是我的解决方案,我相信你可以让它变得更简单,但是你去了:

public class TestHarnessAssignment2 {

    public static void main(String[] args) {

        Counter c1 = new Counter(), c2 = new Counter();

        // comment about things that will print
        System.out.println();
        System.out.println("Initial State");
        System.out.println("counter1" + c1);
        System.out.println("counter2" + c2);
        System.out.println("Counter1 equals Counter2 " + c1.equals((c2)));

        /*
         * c2.subtract1(); c2.subtract1();
         * 
         * System.out.println(); System.out.println(
         * "Error - Attempted to subtract 1 Widget from 0 Widgets.");
         * System.out.println("Number of widgets reset to 0");
         * System.out.println();
         */
        System.out.println();
        System.out.println("Please enter in a number to increment the count by");
        c1.addToCount();
        System.out.println("Thank you!");
        System.out.println();

        System.out.println();
        System.out.println("State after the first test.");
        System.out.println("counter1" + c1);
        System.out.println("counter2" + c2);
        System.out.println("counter1 does not equal counter2 " + c1.equals(c2));

        System.out.println();
        System.out.println("Please enter in a number to decrement the count by");
        c1.subtractFromCount();
        System.out.println("Thank you!");
        System.out.println();
        System.out.println("Please enter in a number to increment the count by");
        System.out.println("Thank you!");
        System.out.println();

        System.out.println();
        System.out.println("State after the second test.");
        System.out.println("counter1" + c1);
        System.out.println("counter2" + c2);
        System.out.println("counter1 equals counter2 " + c1.equals(c2));

    }
}

import java.util.Scanner;

public class Counter {
    // private state variable
    private int counter1;

    // default constructor
    public Counter() {
        counter1 = 0;
    }

    // user arbitrary mutator
    Scanner input = new Scanner(System.in);

    public void addToCount() {
        setCounter1(getCounter1() + input.nextInt());
    }

    // user abitarity mutator 2
    public void subtractFromCount() {
        setCounter1(getCounter1() - input.nextInt());
    }

    // user abi
    // mutator
    public void setCounter1(int newCount) {
        counter1 = newCount;
    }

    // accessor
    public int getCounter1() {
        return counter1;
    }

    public String toString() {
        return " is at " + counter1;
    }

    // sets smamitches
    public int smamitches() {
        return counter1;
    }

    // comment
    public boolean equals(Counter s) {
        return smamitches() == s.smamitches();
    }
}