【Java】【P1957 口算练习题】

package myPackage;
import java.util.Scanner;
public class Main{
    
    
 
	public static void main(String[] args){
    
    
		  Scanner in=new Scanner(System.in);
		  int N=Integer.valueOf(in.nextLine());
		  String[] input =new String[N];
		  String[] output =new String[N];
		  char[] first=new char[N];
		  char[] op=new char[N];
		  int[] op1=new int[N];
		  int[] op2=new int[N];
		  int[] res=new int[N];
		  for(int i=0;i<N;i++) {
    
    
			   input[i]=in.nextLine();
			   String[] splited_str=input[i].split(" ");
			   if(input[i].charAt(0)>='a'&&input[i].charAt(0)<='c') {
    
    
					first[i]=input[i].charAt(0);
				    op1[i]=Integer.valueOf(splited_str[1]);
				    op2[i]=Integer.valueOf(splited_str[2]);
			   }else {
    
    
				    first[i]=first[i-1];
				    op1[i]=Integer.valueOf(splited_str[0]);
				    op2[i]=Integer.valueOf(splited_str[1]);
			   } 
			   switch(first[i]) {
    
    
			   case 'a':{
    
    
				    res[i]=op1[i]+op2[i];
				    op[i]='+';
				    break;
			   }
			   case 'b':{
    
    
				    res[i]=op1[i]-op2[i];
				    op[i]='-';
				    break;
			   }
			   case 'c':{
    
    
				    res[i]=op1[i]*op2[i];
				    op[i]='*';
				    break;
			   }
			   }
			   output[i]=String.valueOf(op1[i] + String.valueOf(op[i]) + op2[i]+"="+res[i]);
		  }
		  for(String out:output) {
    
    
			  System.out.println(out);
			  System.out.println(out.length());
		  }
	}
}


猜你喜欢

转载自blog.csdn.net/m0_57937908/article/details/121847177
今日推荐