ArrayIndexOutOfBoundsException in ten's complement arithmetic implementation

后端 未结 3 1015
被撕碎了的回忆
被撕碎了的回忆 2020-12-20 11:28

My code tries to implement an algorithm to

  • take user input for two integer numbers and an operand + or - from the console,
  • s
3条回答
  •  一生所求
    2020-12-20 12:22

    The code after fixing the problems. all thanks to @kriegaex !

    import java.util.*;
    public class Program9 {
    
      public static String getOperand() {
        Scanner scan = new Scanner(System.in);
        String stringOfInteger;
        
        System.out.print("Please enter an integer up to 50 numbers: ");
        stringOfInteger = scan.nextLine();
        return stringOfInteger;
      }
    
      public static int[] convert(String operand) {
        int [] integer = new int[50];
        char ch;
        
        int position = operand.length() - 1;
        for (int i = integer.length - 1; i >= 0; i--) {
          if (position >= 0)
            ch = operand.charAt(position--);
          else
            ch = 0;
          if (ch >= '0' && ch <= '9') {
            integer[i] = ch - '0';
          } else {
            integer[i] = 0;
          }
        }
        return integer;
      }
    
      public static int[] add(int[] operand1, int[] operand2) {
        int [] result = new int[operand1.length];
        
        int carry = 0;
        for (int i = operand1.length - 1; i >= 0; i--) {
          result[i] = operand1[i] + operand2[i] + carry;
          if (result[i] / 10 == 1) {
            result[i] = result[i] % 10;
            carry = 1;
          } else
            carry = 0;
        }
        return result;
      }
    
      public static int[] complement(int[] operand2){
        int [] result = new int[operand2.length];
        
        for (int i = operand2.length - 1; i >= 0; i--)
          result[i] = 9 - operand2[i];
        return result;
      }
    
      public static int[] add1(int[] operand2){
        int [] result = new int[operand2.length];
        
        result[operand2.length - 1] = 1;
        for (int i = result.length - 2; i >= 0; i--)
          result[i] = 0;
        return result;
      }
    
      public static int[] negate(int[] operand2){
        return add(add1(operand2), complement(operand2));
      }
    
      public static void print(int[] result, String operation) {
        if (operation.charAt(0) == '+')
          System.out.print("The subtotal of the two integers = ");
        else if (operation.charAt(0) == '-')
          System.out.print("The subtraction of the two integers = ");
        
        if (result[0] == 9) {
            result = negate(result);
            System.out.print("-");
        }
        boolean leadingZero = true;
        for (int i = 0; i < result.length; i++) {
          if (leadingZero) {
            if (result[i] == 0)
              continue;
            leadingZero = false;
          }
          System.out.print(result[i]);
        }
        if (leadingZero == true)
          System.out.println('0' - '0');
        System.out.println();
      }
    
      public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int [] result = new int[50];
        String string1 = getOperand();
        String string2 = getOperand();
        int [] integer1 = convert(string1);
        int [] integer2 = convert(string2);
        String operation;
        
        System.out.print("Please enter which operation will be used (+ or -): ");
        operation = scan.nextLine();
        if (operation.charAt(0) == '+')
          add(integer1, integer2);
        else if (operation.charAt(0) == '-')
          integer2 = negate(integer2);
        
        result = add(integer1, integer2);
        
        System.out.println(Arrays.toString(integer1));
        System.out.println(Arrays.toString(integer2));
        System.out.println(Arrays.toString(add(integer1, integer2)));
        print(result, operation);
        
      }
    }

提交回复
热议问题