分隔整数并添加支持负整数的值

时间:2014-03-31 17:17:23

标签: java

我有一个分配将一个整数分解为单个数字,将它们报告回用户并添加它们。我可以这样做,但我正在努力支持负整数。这是我的代码,它完全按照我想要的方式工作,但仅适用于正整数:

import java.util.*;
public class Module4e
{
   static Scanner console=new Scanner(System.in);
   public static void main(String[] args)
   {
        System.out.print("Enter an integer:  ");
        String myNum=console.nextLine();  //Collects the number as a string
        int[] asNumber=new int[myNum.length()]; 
        String []upNum=new String[myNum.length()];  //updated
        int sum=0;  //sum starts at 0
        System.out.println("\n");
        System.out.print("The digits of the number are:  ");

        for (int i=0;i<myNum.length();i++)
        {
            upNum[i]=myNum.substring(i,i+1);
            System.out.print(upNum[i]);
            System.out.print(" ");
            sum=sum+Integer.parseInt(upNum[i]);
        }

        System.out.println("\n");
        System.out.print("The sum of the digits is: ");
        System.out.println(sum);
    }
}

我发现有很多提示可以使用正面整数,但是没有负面效果。

5 个答案:

答案 0 :(得分:0)

// I think you can use this code //also you can multiply the number by -1
int positive = 0;
//positive give you information about the number introduced by the user
if (myNum.charAt(0)=='-'){
     positive=1;
}else{
    positive=0;
for (int i=positive; i<myNum.length(); i++){
    //be carefull with index out of bound exception
    if ((i+1)<myNum.length()){
        upNum[i]=myNum.substring(i,i+1);
    }
}

答案 1 :(得分:0)

将语句String myNum = console.nextLine()更改为String myNum = String.valueOf(Math.abs(Integer.valueOf(console.nextLine())));

答案 2 :(得分:0)

您不必使用String来解决此问题。这是我的想法。

import java.util.*;
public class Module4e throws IllegalArgumentException {
    static Scanner console=new Scanner(System.in);
    public static void main(String[] args) {
        System.out.print("Enter an integer:  ");
        if (!console.hasNextInt()) throw new IllegalArgumentException(); 
        int myNum=console.nextInt();
        myNum = Math.abs(myNum);
        int sum=0;
        System.out.println("\n");
        System.out.print("The digits of the number are:  ");
        While (myNum > 10) {
             System.out.print(myNum % 10);
             System.out.print(" ");
             sum += myNum % 10;
             myNum /= 10;
        }
        System.out.println(myNum);
        System.out.print("The sum of the digits is: ");
        System.out.println(sum);
    }
 }

答案 3 :(得分:0)

试试这个。我给了-51作为输入,输出为-6。这是你在找什么?

import  java.util.*;
public class LoggingApp
{
   static Scanner console=new Scanner(System.in);
   public static void main(String[] args)
   {
       int multiple = 1;      
       System.out.print("Enter an integer:  ");
       String myNum=console.nextLine();  //Collects the number as a string
       Integer myNumInt = Integer.parseInt(myNum);
       if (myNumInt < 1){
           multiple = -1;
           myNum = Integer.toString(myNumInt*-1);
       }
       int[] asNumber=new int[myNum.length()]; 
       String []upNum=new String[myNum.length()];  //updated
       int sum=0;  //sum starts at 0
       System.out.println("\n");
       System.out.print("The digits of the number are:  ");
       for (int i=0;i<myNum.length();i++)
       {
           upNum[i]=myNum.substring(i,i+1);
           System.out.print(upNum[i]);
           System.out.print(" ");
           sum=sum+Integer.parseInt(upNum[i])*multiple;
       }
       System.out.println("\n");
       System.out.print("The sum of the digits is: ");
       System.out.println(sum);
   }
}

答案 4 :(得分:0)

使用RegExp

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

public class TestDigits {

    static Scanner console = new Scanner(System.in);

    public static void main(String[] args) {
        // Validate Input
        String number = console.nextLine();
        Pattern p = Pattern.compile("(-?[0-9]{1})+");
        Matcher m = p.matcher(number);
        if (!m.matches()) {
            throw new IllegalArgumentException("Invalid Numbers");
        }
        // Calculate
        p = Pattern.compile("-?[0-9]{1}+");
        m = p.matcher(number);
        int result = 0;
        System.out.print("The digits of the number are:  ");
        while (m.find()) {
            System.out.print(m.group() + " ");
            result += Integer.valueOf(m.group());
        }
        System.out.println("");
        System.out.println("Result " + result);

    }

}