【问题标题】:adding and subtracting strings in JavaJava中字符串的加减法
【发布时间】:2014-09-02 14:32:15
【问题描述】:

我需要一些关于我应该创建的程序的帮助。我应该创建一个程序来读取用户输入的任意长度的两个字符串,然后将它们相减或相加。我不允许在操作之前将这些字符串转换为数字。这是我到目前为止所得到的。我的老师提到了将字符串转换为 uni-code 以添加和减去它们之类的东西,但我不知道该怎么做,因为我们甚至还没有学习 uni-code。这是我的代码:

import java.util.Scanner;
public class Number {
    private char Sign;
    private String Whole;
    private String Fraction;

    public static void main(String[] args) {
        Scanner Keyboard = new Scanner (System.in);
        System.out.println("This program adds or subtracts numbers of any lengths, please add two numbers: ");
        String num1 = Keyboard.nextLine();
        System.out.println("Enter the second number: ");
        String num2 = Keyboard.nextLine();

        String sum = " ";
        int length = num1.length();
        int carry = 0;

        public Number Add(Number RHS) {

            for (int i = length -1 ; i >= 0; i--) {
                char c1 = num1.charAt(i);
                char c2 = num2.charAt(i);

                int tempSum = (c1 - 48) + (c2 - 48) + carry;
                carry = tempSum / 10;

                int sumDigit = tempSum % 10;
                sum = (char) (sumDigit + 48) + sum;

                if (carry == 1) {
                    sum = "1" + sum;
                }
            }
        }
    }
    public Number (double n) {
        Whole = " ";
        Fraction = " ";
        if (n >= 0) {
            Sign = '+';
        }
        else 
        {
            Sign = '-';
            n = Math.abs(n);
            String numString = new Double(n).toString();
            int position = numString.indexOf(".");
        }

    }
}

【问题讨论】:

  • 您可能希望在左侧填充零,以便它们具有相同的长度。
  • 老实说,我无法理解这个问题,但是我觉得有必要指出,variables 按照惯例是用驼峰写成的,除非它们是 static final 常量,否则永远不要以大写字母开头/跨度>
  • 忘掉 Unicode。 Java 字符串是一个字符序列。 Java 用它们的 Unicode 代码点 来表示字符,但是您不需要知道这一点来完成分配。您只需要知道'0'-'0'==0'1'-'0'==1 等等。
  • 这就是任务,我被困住了。 “你要设计一个 Java 应用程序来对任意长度的数字进行加减运算。一个数字表示为一个对象,其中包括一个符号和两个字符串,分别代表数字的整数部分和小数部分。而且,操作必须是直接加减字符进行。操作前不能将这些字符串转换为数字。
  • "程序必须使用至少包含以下方法的“数字”类": Number();数字(双 n);号码加(号码右轴);数字减法(数字 RHS);字符串 toString();

标签: java string unicode add subtraction


【解决方案1】:
public static String add(String as, String bs){
    ArrayList<String> BigNum = new ArrayList<>();
    int m = as.length()-1;
    int n = bs.length()-1;
    int min = m > n ? n : m ;
    int max = 0;
    String s;
    if(n  > m){
        s = bs;
        max = n;
    }else{s = as ; max = m;}

    Integer carry = 0;
    while(true){
        int a = Integer.parseInt(Character.toString(as.charAt(m)));
        int b = Integer.parseInt(Character.toString(bs.charAt(n)));
        Integer c = a + b + carry;
        if(c > 9){
            carry = 1;
            c %=10;
        }else carry = 0;

        BigNum.add(c.toString());
        n--; m--; min--; max--;

        if ( min < 0   ) {
            if(carry !=0 && max < 0 )
                BigNum.add(carry.toString());
            break;
        }
    }
    Integer c = carry;
    while(max >= 0) {
        c += Integer.parseInt(Character.toString(s.charAt(max)));
        BigNum.add(c.toString());
        c = 0;
        max--;
    }
    String s2 = "";
    for (int i = BigNum.size()-1; i >= 0; i--) {
        s2 +=BigNum.get(i);
    }
    return s2;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    相关资源
    最近更新 更多