如何找到前三位数之和

时间:2016-06-13 20:35:16

标签: java loops if-statement while-loop

我是Java的新手,我试图在int数字中找到前三位的总和。 我有一个数字123456我想找到前三位数的总和123,然后找到最后三位数的总和456,然后进行比较。我无法理解如何做到这一点。我接下来编码:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the number:");
    int number = scanner.nextInt();
    int length = (int) Math.ceil(Math.log10(number));

    for (int i = 1; i <= 3; i++) {
        System.out.println(i);
    }

    System.out.println(length);
    System.out.println(number);
}

我认为我应该使用for循环,但我不确定。我知道如何计算一个数字的长度。但是如何找到前三个数字和最后三个数字之和?

5 个答案:

答案 0 :(得分:3)

假设你的长度是恒定的(6):

String first = (""+number).substring(0, 3);
String second = (""+number).substring(3, 6);
int firstSum = 0;
int secondSum = 0;
for (int x = 0; x < first.length; x++)
    firstSum += Integer.parseInt(first.charAt(x)+"");
for (int x = 0; x < second.length; x++)
    secondSum += Integer.parseInt(second.charAt(x)+"");
System.out.println("Sum of first 3: " + firstSum);
System.out.println("Sum of second 3: " + secondSum);

阅读String API - 它有很多有用的方法。对于像这样的小程序,通常很容易转换为String,使用String方法,然后解析回int。

编辑:Andy Turner和Jeremy Kato向我通报了Character.getNumericValue()。因此,声明:

firstSum += Integer.parseInt(first.charAt(x)+"");

可以更改为:

firstSum += Character.getNumericValue(first.charAt(x));

同样适用于secondSum

答案 1 :(得分:1)

首先,可能应该纠正你的长度拼写 - 虽然可能没有,因为你真的不需要那个变量来做到这一点。

您的代码应该做些什么来使您更容易立即将输入转换为int。您首先要将输入字符串分成两部分,然后取两个数字并用循环求它们。

以下是我对自己所做的事情的看法:

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the number:");
    String input = scanner.nextLine();

    String firstHalf = input.substring(0, 3); // note that depending on how 
                // many digits you want to change, these numbers must change as well.
    String secondHalf = input.substring(3); 

    int sum1 = 0;
    int sum2 = 0;


    // don't forget: start at 0, not 1!
    for(int i = 0; i<=3; i++){

        sum1 += Character.getNumericValue(firstHalf.charAt(i));     
        sum2 += Character.getNumericValue(secondHalf.charAt(i));
        // this call to the Character class converts a character to an int. 

    }

    System.out.println(sum1);
    System.out.println(sum2);

}

请注意,此代码可用于输入6个字符,并假设它们将成为数字。如果您输入的字符太少或者如果您输入字母,它会崩溃,但您可以随时担心。

答案 2 :(得分:0)

这是一种直接的方法,假设您的整数具有偶数位数......

public class Test {
    public static void main(String[] args) {
        // Makes the assumption that the input integer contains an even number of digits.
        int x = 123456;
        String s = Integer.toString(x);
        int[] count = new int[2];
        for (int i = 0; i < s.length(); i++) {
            int digit = Integer.parseInt(s.substring(i, i + 1));
            int countIndex = (int)Math.floor(i * 2 / s.length());
            count[countIndex] += digit;
        }

        int half = s.length() / 2;
        System.out.println("First Sum of " + s.substring(0, half) + ": " + count[0]);
        System.out.println("Second Sum of " + s.substring(half) + ": " + count[1]);
    }
}

替代方法

这是另一种不使用上述数组的方法,实际上通过将总和抽象为方法来简化逻辑。

public class Test {
    public static void main(String[] args) {
        // Makes the assumption that the input integer contains an even number of digits.
        int x = 123456;
        String s = Integer.toString(x);
        int half = s.length() / 2;
        String firstHalf = s.substring(0, half);
        String secondHalf = s.substring(half);
        System.out.println(firstHalf);
        System.out.println(secondHalf);
        int sum1 = sumString(firstHalf);
        int sum2 = sumString(secondHalf);
        System.out.println("Sum 1: " + sum1);
        System.out.println("Sum 2: " + sum2);
    }

    private static int sumString(String s) {
        int sum = 0;
        for (int i = 0; i < s.length(); i++) {
            sum += Integer.parseInt(s.substring(i, i + 1));
        }

        return sum;
    }
}

答案 3 :(得分:0)

以下是一种不使用字符串或数组的替代解决方案:

public static void main(String args[]){
    int num=123456;
    int second = 0;
    int first = 0;      

    second = addLastThree(num);
    num /= 1000;
    first = addLastThree(num);

    System.out.println("first -- > " + first);
    System.out.println("second -- > " + second);


}

static int addLastThree(int num){
    int sum = 0;
    for (int i =0; i<3; i++){
        sum += (num/Math.pow(10,i)) % 10;
    }   
    return sum;
}   

答案 4 :(得分:0)

您可以尝试使用此代码,并在评论中解释每一行。

public static void main(String[] args) {
    Scanner scanner = new Scanner(System.in);
    System.out.println("Enter the number:");
    int number = scanner.nextInt();
    int n=number; //duplicating the value for the reference.
int l = (int) Math.log10(number) + 1; //Length of the number
int half = l/2; //to find half length of the number to sum first and last digits seperately.
int last_Sum = 0; //to store sum of last digits
int first_Sum = 0; //to store sum of first digits
for(int i=0; i<half && number>0; i++){ //to sum last digits
    last_Sum = last_Sum + number%10;
    number = number/10;
}for(int i=0; i<half && number>0; i++){ //to sum first digits
    first_Sum = first_Sum + number%10;
    number = number/10;
}
System.out.println("Length of the number "+l);
System.out.println("Number is "+n);
System.out.println("Sum of last digits "+last_Sum);
System.out.println("Sum of first digits "+first_Sum);}}