Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/304.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_String_Add_Subtraction - Fatal编程技术网

Java中字符串数的加减

Java中字符串数的加减,java,string,add,subtraction,Java,String,Add,Subtraction,我想创建一个程序,用户输入两个数字,可以是负数,也可以是正数,可以包含小数点,也可以不包含小数点。从理论上讲,当你加上“256.78+78.6783”时,它应该像一个普通的加法问题一样进行运算并完成运算 我已经知道如何在任何长度的数字都是正数的情况下进行加法,这花了我很多时间,但是当我加负数,甚至减去这个数字时,我并没有得到正确的结果。这应该适用于用户输入的任何一组两个数字 这是我目前的代码,有什么建议吗? 另外,在操作之前,我不允许将这些数字转换为int或double,因此无法解析它们 pub

我想创建一个程序,用户输入两个数字,可以是负数,也可以是正数,可以包含小数点,也可以不包含小数点。从理论上讲,当你加上“256.78+78.6783”时,它应该像一个普通的加法问题一样进行运算并完成运算

我已经知道如何在任何长度的数字都是正数的情况下进行加法,这花了我很多时间,但是当我加负数,甚至减去这个数字时,我并没有得到正确的结果。这应该适用于用户输入的任何一组两个数字

这是我目前的代码,有什么建议吗?
另外,在操作之前,我不允许将这些数字转换为
int
double
,因此无法解析它们

public class Number {
    static Scanner kbd = new Scanner (System.in);
    private String sign; 
    private String whole;
    private String decimal;
    private String fraction;
    private static double firstNumber;
    private static double secondNumber;

    public static void main(String[] args) {

        System.out.println("Please enter the first number: ");
        firstNumber = kbd.nextDouble();
        System.out.println("Next, enter the second number: ");
        secondNumber = kbd.nextDouble();

        Number x = new Number (firstNumber);
        Number y = new Number (secondNumber);
        Number sum = x.add(y);
        System.out.println("x = " + x);
        System.out.println("y = " + y);
        System.out.println("x + y = " + sum);
        Number subtract = x.subtract(y);
        System.out.println("x - y = " + subtract);
    }
    public Number() 
    {
        whole = "0";
        decimal = "0";
        sign = "+";
    }
    public String toString() 
    {
        return sign + whole + "." + decimal;
    }
    public Number (double n) 
    {
        whole = "0";
        decimal = "0";
        sign = "+";

        String numString = new Double(n).toString();
        if (numString.charAt(0) == '-') {
            sign ="-";
            numString = numString.substring(1);
        }
        int position = numString.indexOf(".");
        if (position == -1)
            whole = numString;
        else
        {
            whole = numString.substring(0,position);
            decimal = numString.substring(position+1);
            fraction = "";
        }
    }

    public Number add (Number RHS) {
        this.fixWhole (RHS);
        this.fixDecimal(RHS);
        return this.addNum (RHS);
    }
    public Number subtract (Number RHS) {
        this.fixWhole(RHS);
        this.fixDecimal(RHS);
        return this.subtractNum (RHS);
    }
    private void fixWhole (Number RHS) {
        int firstWholeNum = this.whole.length();
        int secondWholeNum = RHS.whole.length();
        int difference = firstWholeNum - secondWholeNum;
        if (difference > 0) {
            for (int i = 1; i <= difference; i++) 
                RHS.whole = "0" + RHS.whole;
        }
        else if (difference < 0 ) {
            difference = Math.abs(difference);
            for (int i = 1; i <= difference; i++)
                this.whole = "0" + this.whole;
        }
    }

    private void fixDecimal  (Number RHS ) {
        int firstDecimalNum = this.decimal.length();
        int secondDecimalNum = RHS.decimal.length();
        int difference = firstDecimalNum - secondDecimalNum;

        if (difference > 0) {
            for (int i = 1; i <=  difference; i++)
                RHS.decimal = RHS.decimal + "0";
        }
        else if (difference < 0 ) 
        {
            difference = Math.abs(difference);
            for (int i = 1; i <= difference; i ++)
                this.decimal = this.decimal + "0";
        }
    }

    private Number addNum (Number RHS ) {
        Number sum = new Number();
        sum.decimal = "";
        int carry = 0;
        int decimalNum = this.decimal.length();
        for (int i = decimalNum - 1; i >= 0; i --) {
            char c1 = this.decimal.charAt(i); 
            char c2 = RHS.decimal.charAt(i); 
            int tempSum= (c1 - 48) + (c2 - 48) + carry;
            carry =  tempSum/ 10;
            int sumDigit = tempSum % 10;
            sum.decimal = (char) (sumDigit + 48) + sum.decimal;
        }
        sum.whole = "";
        int wholeNum = this.whole.length();
        for (int i = wholeNum - 1; i >= 0; i --) {
            char c1 = this.whole.charAt(i);
            char c2 = RHS.whole.charAt(i);
            int tempSum = (c1 - 48) + (c2 - 48 ) + carry;
            carry = tempSum / 10;
            int sumDigit = tempSum % 10;
            sum.whole = (char) (sumDigit + 48) + sum.whole;
        }
        if (carry != 0) 
            sum.whole = "1" + sum.whole;
        return sum;
    }

    private Number subtractNum (Number RHS ) {
        Number sum = new Number();
        sum.decimal = "";
        int carry = 0;
        int decimalNum = this.decimal.length();
        for (int i = decimalNum - 1; i >= 0; i --) {
            char c1 = this.decimal.charAt(i); 
            char c2 = RHS.decimal.charAt(i); 
            int tempSum= (c1 - 48) - (c2 - 48) - carry; 
            carry =  tempSum/ 10;
            int sumDigit = tempSum % 10;
            sum.decimal = (char) (sumDigit - 48) + sum.decimal;
        }
        sum.whole = "";
        int wholeNum = this.whole.length();
        for (int i = wholeNum - 1; i >= 0; i --) {
            char c1 = this.whole.charAt(i);
            char c2 = RHS.whole.charAt(i);
            int tempSum = (c1 - 48) - (c2 - 48 ) + carry;
            carry = tempSum / 10;
            int sumDigit = tempSum % 10;
            sum.whole = (char) (sumDigit + 48) + sum.whole;
        }
        if (carry != 0) 
            sum.whole = "1" + sum.whole;
        return sum;
    }
}
公共类编号{
静态扫描仪kbd=新扫描仪(System.in);
私有字符串符号;
私有字符串整体;
私有字符串十进制;
私有字符串分数;
私有静态双首数;
私有静态双秒数;
公共静态void main(字符串[]args){
System.out.println(“请输入第一个数字:”);
firstNumber=kbd.nextDouble();
System.out.println(“下一步,输入第二个数字:”);
secondNumber=kbd.nextDouble();
编号x=新编号(第一个编号);
编号y=新编号(第二个编号);
数字和=x。加(y);
System.out.println(“x=”+x);
System.out.println(“y=“+y”);
System.out.println(“x+y=“+sum”);
数字减法=x.减法(y);
System.out.println(“x-y=“+subtract”);
}
公众号码()
{
整型=“0”;
decimal=“0”;
符号“+”;
}
公共字符串toString()
{
返回符号+整数+“+”小数;
}
公共号码(双n)
{
整型=“0”;
decimal=“0”;
符号“+”;
String numString=new Double(n).toString();
if(numString.charAt(0)=='-'){
符号“-”;
numString=numString.substring(1);
}
int position=numString.indexOf(“.”);
如果(位置==-1)
整=numString;
其他的
{
整=numString.substring(0,位置);
十进制=numString.子字符串(位置+1);
分数=”;
}
}
公共编号添加(编号RHS){
这是一个整体(RHS);
这个.固定十进制(RHS);
返回this.addNum(RHS);
}
公共数字减法(数字RHS){
这是一个整体(RHS);
这个.固定十进制(RHS);
返回此.subtractNum(RHS);
}
私有无效固定整数(数字RHS){
int firstWholeNum=this.whole.length();
int secondWholeNum=RHS.whole.length();
整数差=第一个整数-第二个整数;
如果(差异>0){
对于(int i=1;i=0;i--){
char c1=此.decimal.charAt(i);
字符c2=右十进制字符(i);
int tempSum=(c1-48)-(c2-48)-进位;
进位=tempSum/10;
int-sumDigit=tempSum%10;
sum.decimal=(char)(sumdigital-48)+sum.decimal;
}
sum.total=“”;
int wholeNum=this.total.length();
对于(int i=wholeNum-1;i>=0;i--){
char c1=这个.整体.charAt(i);
字符c2=右全字符(i);
int tempSum=(c1-48)-(c2-48)+进位;
进位=tempSum/10;
int-sumDigit=tempSum%10;
sum.whole=(字符)(sumDigit+48)+sum.whole;
}
如果(进位!=0)
sum.whole=“1”+sum.whole;
回报金额;
}
}

将两个数字都作为字符串,并将符号存储到符号字符串中相应的数字对象中,然后像

System.out.println("Please enter the first number: ");
        firstNumber = kbd.nextString();
        System.out.println("Next, enter the second number: ");
        secondNumber = kbd.nextString();

        Number x = new Number (firstNumber.substring(1),firstNumber.charAt(0));
        Number y = new Number (secondNumber.substring(1),secondNumber.charAt(0));

/*convert the firstNumber.substring(1) and secondNumber.substring(1) to doubles using  Double.parseDouble()*/

public String doTheOperation(Number other){
 if(this.sign.equals(otherNumber.sign)){
  /*simply the double values and put the sign*/ in front of it and return it
 }

 else{
  do the simple double subtraction and by looking at your code i believe you can find out the bigger double among them
 }
}

我不知道你不能解析它们是什么意思。你需要以这种或那种方式解析字符串,事实上,在你当前的方法中,你已经在解析数字了。@for3st:不完全正确。最终,是的,在某个时候字符串输入需要转换为“数字”,即使只是因为它需要加/减。但是OP的赋值是nt显然是一次只转换一个字符。这不是大规模转换为浮点数的情况。此任务的目的似乎是从头开始设计BigNum功能。
(char)(sumDigit-48)+sum.decimal;
使用
+
sumDigit+48
。(为了清晰起见,您还可以执行
sumDigit+'0'
)。您还可以提供英文(非java)的减法算法?在执行减法时,显然存在错误。通过改变符号,可以将每个
+
-
操作转换为两个正值的加法或两个正值的减法,其中分钟数不小于减数。(如果您更改符号,则需要确保结果具有正确的符号)嗨,我不应该用parse来做运算,我的任务是一个字符一个字符地做算术。另外,你的意思是做输入检查吗?如果用户输入负数,我调用对负数进行运算的方法。你能找出哪个数更大,哪个更小吗?如果不能,我会告诉你逻辑是这样的,但是先告诉我,你能找出答案吗?如果你能找出较大的那个,那么答案很容易告诉你,所以你只需要专注于符号,如果符号不同,从较大的符号中减去较小的一个事实上,不,我不知道怎么做。你可能会对循环做些什么吗?我真的被卡住了,有什么帮助吗非常感谢!好的,按照我在回答中所显示的字符串形式读取数字,现在按照步骤1获得较大的数字。假设您有两个数字,即n1和n2,那么现在检查数字