为啥这个只执行第一个双加法啊,是这样写不对么

img

img


这里因为提交不通过删了
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
double x=0.0,y=0.0,z=0.0;
double result=0.0;

    System.out.println("第一个数:");
    x=scanner.nextDouble();
    System.out.println("运算符号:");
    String i=scanner.next();
    System.out.println("第二个数:");
    y=scanner.nextDouble();
    System.out.println("运算符号:");
    String j=scanner.next();
    System.out.println("第三个数:");
    z=scanner.nextDouble();
    
    if(i.equals("+")||j.equals("+"))
    {result=jiajia(x,y,z);}
    else if(i.equals("+")||j.equals("-"))
    {result=jiajian(x,y,z);}
    else if(i.equals("+")||j.equals("*"))
    {result=jiacheng(x,y,z);}
    else if(i.equals("+")||j.equals("/"))
    {result=jiachu(x,y,z);}
    System.out.println(result);
}
public static double jiajia(double a,double b,double c){
    return a+b+c;
}
public static double jiajian(double a,double b,double c){
    return a+b-c;
}
public static double jiacheng(double a,double b,double c){
    return a+(b*c);
}
public static double jiachu(double a,double b,double c){
    return a+(b/c);
}

}

这是 或者 ,两个有一个成立就可以的,改成 &&

img

  • 你可以参考下这个问题的回答, 看看是否对你有帮助, 链接: https://ask.csdn.net/questions/7701695
  • 这篇博客你也可以参考下:给定一个正整数数组,返回这个数组元素拼接起来所能组成的最大数
  • 除此之外, 这篇博客: 关于实现超长整数运算中的 超长整数的加减法运算 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • 我们所熟知的基本数据类型中,long long 超长整型是表达最大整数的数据类型,但是当我们处理超过这个类型的数据范围时,我们可以用顺序串来处理超长整数的运算,下面我将举例200位以内的整数加减法的实现过程

    #include <stdio.h>
    #include <string.h>
    #define MaxSize 200
    typedef struct figure{
    	char data[MaxSize];     //存放整数(逆序存放) 
    	int len;                //记录整数长度 
    }Count;
    

    以上代码,用来声明存放超长整数的数据结构

    int main(){
    	Count front, behind; 
    	input(&front,&behind);
    	add(&front,&behind); 
    	sub(&front,&behind);
    	return 0;
    }
    void input(Count *front, Count *behind){   //输入两个整数 
    	char first[MaxSize], second[MaxSize];
    	int i, n = 0;
    	printf("请输入两个超长整数(分行输入):\n");
    	fflush(stdin);
    	gets(first);
    	gets(second);
    	for(i = strlen(first) - 1; i >= 0; i--){
    		front->data[n++] = first[i];                   //逆序存放前者整数 
    	}
    	front->data[n] = '\0';
    	front->len = strlen(first);
    	for(i = strlen(second) - 1, n = 0; i >= 0; i--){
    		behind->data[n++] = second[i];                  //逆序存放后者整数 
    	}
    	behind->data[n] = '\0';
    	behind->len = strlen(second); 
    }
    

    这里需要注意:我们实际存入的整数在顺序串中是逆序存放的,例如输入
    123323<回车>
    2323<回车>
    front->data字符数组中为: 323321\0
    behind->data字符数组中为:3232\0

    void add(Count *front, Count *behind){  //加法运算 
    	int longer, i, first, second, ahead = 0;
    	longer = front->len > behind->len ? front->len : behind->len;
    	char result[longer+1]; //相加结果的长度等于较长整数长度加1 
    	for(i = 0; i < longer; i++){
    		if(i > front->len - 1) first = 0;
    		 else first = (int)front->data[i] - (int)'0';
    		if(i > behind->len - 1) second = 0;
    		 else second = (int)behind->data[i] - (int)'0';
    		result[i] = (first + second + ahead) % 10 + '0';
    		if(first + second + ahead >= 10)  ahead = 1; 
    		 else ahead = 0;
    		if(ahead == 1 && i == bigger - 1){
    			result[++i] = '1';
    		}
    	}
    	result[i] = '\0';
    	printf("相加得:");
    	for(i = strlen(result) - 1; i >= 0; i--){  //逆序输出 
    		printf("%c",result[i]);
    	}
    }
    

    在进行算法之前,我们需要总结出加法的运算规律,还有考虑容易被我们忽略的结果(下面是模拟字符数组中的运算过程):
    (示例一):不同长度整数之和
    2 3 4
    4 8 6 4 3 +
    ——————
    6 1 1 5 3
    也就是432 + 34684 = 35116
    (示例二):同长度,结果进位的情况
    9 9 9
    6 6 6 +
    ——————
    5 6 6 1
    也就是999 + 666 = 1665

    void sub(Count *front, Count *behind){   //减法运算 
    	int first, second, i, flag = 0;  //flag = 0表示正数 
    	if(front->len == behind->len){
    		for(i = front->len - 1; i >= 0; i--){
    		   if(front->data[i] < behind->data[i]){
    		   	    flag = 1;
    		   	    sub_deal(behind,front,flag);              //同长度,并且前者比后者小,结果为负数,flag = 1 
    		   	    break;
    		   } 
    		} 
    		if(flag != 1) sub_deal(front,behind,flag);        //同长度,并且前者比后者大,结果为正数,flag = 0 
    	}
    	if(front->len > behind->len)  sub_deal(front,behind,0);       //前者比后者长度大 ,说明结果为正,flag = 0
    	else if (front->len < behind->len) sub_deal(behind,front,1);  //前者比后者长度小,结果为负数 
    }
    void sub_deal(Count *a, Count *b, int flag){ //处理较大的数与较小的数相减的问题,实现算法 
    	int i = 0, back = 0, first, second, longer;
    	char result[a->len+1];
    	longer = a->len;
    	while(i < longer){
    		if(i > b->len - 1) second = 0; //超出较小整数长度的,second赋值为0,符合运算法则 
    		else second = (int)b->data[i] - (int)'0';   
    		first = (int)a->data[i] - (int)'0';
    		if(first - second + back < 0){
    			first = (int)a->data[i] - (int)'0' + 10;
    			result[i++] = (first - second + back) + '0';
    			back = -1;
    		} 
    		else{
    			result[i++] = (first - second + back) + '0';
    			back = 0;
    		}
    	}
    	if(flag == 1){            //如果flag = 1 表示相减的结果是负数,需要加一个'-'字符 
    		result[i++] = '-';
    	}
    	result[i] = '\0';
    	printf("\n相减得:");
    	for(i = strlen(result) - 1; i >= 0; i--){
    		printf("%c",result[i]);
    	} 
    }                       
    

    同样地,减法运算考虑的情况比加法多得多,在该程序中支持较小整数减较大整数的情况,在实际操作减法中,无论是哪种情况,我们发现:
    (1) 199 - 200 = - (200-199)
    (2) 19 - 4343 = -(4343 - 19)
    (3) 200 - 199 = +(200 - 199)
    参考上述例子,我们得出我们仅仅只需要控制结果的正负,剩下的按照较大数减去较小数所得出的结果,然后在结果的前面加上正负号即可,所以我们使用了sub_deal()函数大大简化了程序代码的重复性。

    运行结果:
    在这里插入图片描述

    在这里插入图片描述
    总得来说:发现运算法则的规律,考虑所有出现的情况,归纳其共同点.利用变量或函数间接表达同一性,达到简化程序、优化程序的效果!

  • 您还可以看一下 任大勇老师的数据分析思维及方法课程中的 归集数据指标,数据指标拆解与确定数据维度小节, 巩固相关知识点