Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 字符串数加法_Java - Fatal编程技术网

Java 字符串数加法

Java 字符串数加法,java,Java,这是我学习Java的第二周,我正在尝试实现我在书中看到的一些函数 对于第一个函数,我希望将整数表示为整数数字数组,这样就可以绕过整数值的范围 例如: 第一功能 输入:整数输入(123456) 输出:integerString[]={1,2,3,4,5,6} 第二功能 输入:stringInput[]={123456} 输出:integerString[]={1,2,3,4,5,6} 第三种功能: (我想像普通加法一样添加2个整字符串值) 输入:integerString1[]={1,2,3,4

这是我学习Java的第二周,我正在尝试实现我在书中看到的一些函数

对于第一个函数,我希望将整数表示为整数数字数组,这样就可以绕过整数值的范围

例如:


第一功能 输入:整数输入(123456)

输出:integerString[]={1,2,3,4,5,6}

第二功能 输入:stringInput[]={123456}

输出:integerString[]={1,2,3,4,5,6}

第三种功能: (我想像普通加法一样添加2个整字符串值)

输入:integerString1[]={1,2,3,4,5,6}integerString2[]={1,2,3,4,5,6}

输出:[2,4,6,9,1,2]


这就是我到目前为止所拥有的,它充满了错误,我所能得到的只是一个空输出。非常感谢您的帮助

谢谢

public class tryingOut{
    
    public static int[] integerToNumberArray(int value) {
        String temp = Integer.toString(value);
        int[] list = new int[temp.length()];
        for (int i = 0; i < temp.length(); i++) {
            list[i] = temp.charAt(i) - '0';
        }
        return list;
    }
    
    public static boolean isNumeric(String value) {
        if (value == null) {
            return false;
        }
        try {
            int d = Integer.parseInt(value);
        } catch (NumberFormatException nfe) {
            return false;
        }
        return true;
    }
    
    public static int[] stringToNumberArray(String value) {
        
        boolean checkNumeric = isNumeric(value);
        
        if (checkNumeric = false) {
            return null;
        } else {
            String[] items = value.replaceAll("\\[", "").replaceAll("\\]", "").replaceAll("\\s", "").split(",");
            
            int[] results = new int[items.length];
            
            for (int i = 0; i < items.length; i++) {
                try {
                    results[i] = Integer.parseInt(items[i]);
                } catch (NumberFormatException nfe) {
                    
                }
            }
            return results;
        }
    }
    
    public static int[] addNumberArrays(int[] a1, int[] a2) {
        int[] result = null;
        
        return result;
    }
    
    public static void main(String[] args) {
        
        int test1 = 19;
        int test2 = 823;
        int[] list1 = integerToNumberArray(test1);
        int[] list2 = integerToNumberArray(test2);
        
        //  stringToNumberArray(test1);
        //  stringToNumberArray(test2);
        
        
    }
}
公共类尝试退出{
公共静态int[]integerToNumberArray(int值){
字符串温度=整数。toString(值);
int[]list=新int[temp.length()];
对于(int i=0;i
欢迎来到JAVA编程的世界。首先,我感谢您对编程的热情和所做的努力

正如您在评论中提到的,您可以通过以下方式实现此目的。然而,我喜欢你自己尝试这一点的兴趣,因此我将尝试在下面分享一个代码,尽可能多的描述我可以提供给你,让你理解


要指出原始代码中的几个错误:

  • 对于方法
    isNumeric(字符串值)
    您不需要使用
    if条件单独处理
    null
    ,因为
    try-catch
    块将自动为您处理它

  • 对于
    stringToNumberArray(String-value)
    方法,您不需要使用
    String.split
    ,而且在
    String[]items=…]行中您做了什么也很不清楚


  • 完整代码:

    public class PlayingWithInt {
        
        public static boolean isNumeric(String value) {
            try {
                Integer.parseInt(value);
            } catch (NumberFormatException nfe) {
                // This handles both null and other non int values so we don't need an extra condition to check for null
                return false;
            }
            return true;
        }
        
        // Assuming argument value is "123456"
        public static int[] stringToNumberArray(String value) {
            if (isNumeric(value)) { // We generally don't write `== true` or `== false` with boolean values
                char[] valueAsCharArray = value.toCharArray(); // valueAsCharArray = {'1', '2', '3', '4', '5', '6'}
                int[] valueAsIntArray = new int[valueAsCharArray.length]; // valueAsIntArray = {0, 0, 0, 0, 0, 0}
                
                for (int i = 0; i < valueAsCharArray.length; i++) {
                    valueAsIntArray[i] = valueAsCharArray[i] - '0';
                }
                return valueAsIntArray; // valueAsCharArray = {1, 2, 3, 4, 5, 6}
            }
            return null; // Null will be returned if the value is not numeric
        }
        
        // Assuming argument value is {"1", "2", "3", "4", "5", "6"}
        public static int[] stringToNumberArray(String[] value) {
            int[] valueAsIntArray = new int[value.length]; // valueAsIntArray = {0, 0, 0, 0, 0, 0}
            
            for (int i = 0; i < value.length; i++) {
                valueAsIntArray[i] = value[i].charAt(0) - '0';
            }
            return valueAsIntArray; // valueAsCharArray = {1, 2, 3, 4, 5, 6}
        }
        
        // Let's say value is 123456
        public static int[] integerToNumberArray(int value) {
            String valueAsString = Integer.toString(value); // valueAsString = "123456"
            return stringToNumberArray(valueAsString); // We are using our already built method that does the same thing.
        }
        
        public static int[] addNumberArrays(int[] a1, int[] a2) {
            int maxLength;
            if (a1.length > a2.length) maxLength = a1.length;
            else maxLength = a2.length;
            
            int[] result = new int[maxLength + 1];
            
            int i = a1.length - 1; // Index iterator for a1
            int j = a2.length - 1; // Index iterator for a2
            int k = result.length - 1; // Index iterator for result
            int carry = 0; // carry to handle case when the sum of two digits more than deci/ ten (10)
            
            while (i >= 0 && j >= 0) {
                int sum = a1[i] + a2[j] + carry;
                result[k] = sum % 10;
                carry = sum / 10;
                i--;
                j--;
                k--;
            }
            
            // If a1 has more digits than a2
            while (i >= 0) {
                int sum = a1[i] + carry;
                result[k] = sum % 10;
                carry = sum / 10;
                i--;
                k--;
            }
            
            // If a2 has more digits than a1
            while (j >= 0) {
                int sum = a2[j] + carry;
                result[k] = sum % 10;
                carry = sum / 10;
                j--;
                k--;
            }
    
            result[0] = carry;
        
            return result;
        }
        
        public static void main(String[] args) {
            int test1 = 123456;
            int test2 = 123456;
            int[] num1 = integerToNumberArray(test1);
            int[] num2 = integerToNumberArray(test2);
            int[] result = addNumberArrays(num1, num2);
            
            for (int i : result) System.out.print(i); // This is known as enhanced-for-loop or for-each-loop
            
            System.out.println();        
        }
    }
    

    其他输入和输出: 示例1

    代码:

    输出:

    0246912
    
    0842
    
    0842
    
    1998
    
    示例2

    代码:

    输出:

    0246912
    
    0842
    
    0842
    
    1998
    
    示例3

    代码:

    输出:

    0246912
    
    0842
    
    0842
    
    1998
    

    参考资料:

  • “满是错误,我只能得到一个空输出”您应该告诉我们至少一个错误的详细信息,以及您希望打印哪行代码的输出?您知道如何使用调试器吗?请将您的帖子限制在一个函数示例中,顺便说一句,包括您在其中遇到的错误,要处理非常大的整数,请使用Java内置的类。这并不困难,但比您可能意识到的更困难。例如,您需要处理从一个添加到下一个添加的结转。因为6+6等于12,所以你需要在数组中放入2,并将1带入下一个加法。这也意味着,如果索引0添加到生成进位的位置,您需要决定如何处理它,因为您不能将其进位到-1的位置。因此,您要么复制数组,要么声明溢出。非常感谢您的详细解释。这让我的每一个概念都很清晰。不客气。如果你觉得这有帮助,别忘了投票<编码>快乐编码