Computer integer class x, Java writes a computer class to implement the addition, subtraction, multiplication, and division operations of two integers. How to implement…

Satisfactory answer luuky51812 2013.01.04 Adoption rate: 59% Level: 7 Already helped: 960 people Class Computer { int a,b; int add(int a,int b){return a+b;} int sub(int a,int b){return a-b;} int mul(int a,int b){return a*b;} double div(int a,int b){return double(a)/double(b);} public static void main(String args[]) { Computer cm= new Computer(); int a=100,b=35; System.out.println(“a+b=”+cm.add (a,b)); System.out.println(“a-b=”+cm.sub(a,b)); System. out.println(“a*b=”+cm.mul(a,b)); System.out.println(“a/b=&# 8221;+cm.div(a,b)); } } 00Share report

Let’s talk about several common operations on dates in Java—value acquisition, conversion, addition, subtraction, and comparison.

Let’s talk about several common operations on dates in Java—value acquisition, conversion, addition, subtraction, and comparison.

In the development process of Java, it is inevitable to get entangled with the Date type,I am going to summarize the date-related operations frequently used in the project,JDK version 1.7,If it can help everyone save a few minutes to get up and move, #xff0c; Go make a cup of coffee , it will be great, Hehe. Of course, “I only provide feasible solutions” and do not guarantee best practices. Discussions are welcome. 1. Date value In the era of the old version of JDK, there were many codes that used the java.util.Date class to obtain date values. ,However, since the Date class is not convenient for internationalization, in fact, starting from JDK1.1, it is more recommended to use the java.util.Calendar class for time and date processing. The operations of the Date class will not be introduced here – let us go straight to the topic – how to use the Calendar class to obtain the current date and time. Since Calendar’s constructor method is protected, we will create a Calendar object through the getInstance method provided in the API. 1 //There are multiple overloaded methods to create Calendar objects2 Calendar now = Calendar.getInstance(); //Default3 // Specify time zone and region,…

Introduction to Java 200 Example 03: Addition, subtraction, multiplication and division of floating point numbers

About the author Author name: Programming World Ming Shiyin Introduction: CSDN blog expert, engaged in software development Over the years, I have been proficient in Java and Javascript. I have also learned and grown step by step from scratch. I fully understand the importance of learning and accumulation. I like to play and upgrade with the majority of ADCs. You are welcome to pay attention and look forward to learning, growing and taking off with you. ! Introduction Many Java beginners ask me, novices are obviously very attentive and study hard. Java knowledge, turned around and forgotten, is very frustrating. How can a novice grow up quickly and become a great expert? In fact, there is only one skill to become a master: “learn more and practice more”, so Mingge compiled some typical practice examples. Through practice, you can quickly improve your coding skills and proficiency, so that you can become a master on the way. Gone and never come back (remember to practice with your own hands)! Navigation ✪ Introduction to Java 200 Example Series Directory Index

java study notes – java method 04. Custom method + switch statement to implement addition, subtraction, multiplication and division of two numbers

java study notes – java method 04. Custom method + switch statement to implement addition, subtraction, multiplication and division of two numbers

Simple Calculator Intended functions: Realize addition, subtraction, multiplication and division functions, and Able to receive new data in a loop, implemented through user interaction. Recommended ideas: Write four methods: addition, subtraction, multiplication and division Use loop + switch for user interaction Pass the two numbers that need to be operated Output results The code is as follows: import java.util.Scanner;public class calculation { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); double a =0.0; double b=0.0; String s=null; System.out.println(“Please enter the formula:”); a = scanner.nextDouble(); s = scanner.next(); b = scanner.nextDouble(); System.out.println(“The calculation result is: “); switch (s){ case “+ “: add(a,b); break; case “-“: sub(a,b); break; case “*”: mul(a,b); break; case “/”: dev(a,b); break; } scanner.close(); } public static void add(double a,double b){ System.out.println(a+”+”+b+”=”+(a+b)); } public static void sub(double a,double b){ System.out.println(a+”-“+b+”=”+(a-b)); } public static void mul(double a,double b){ System.out.println(a+”*”+b+”=”+(a*b)); } public static void dev(double a,double b) { System.out.println(a+”/”+b+”=”+(a/b)); }} Run results: Thereareseveralproblems: ThereareonlytwooperandsbydefaultandTheoperatorassignsvalues​​sequentiallybetweenthetwooperands.Howtousealooptoinputarithmetic,thatis,acharacterissandwichedbetweentwonumbers.Cantheinputusingaloopdistinguishbetweennumbersandcharacters,andhowtoassignvalues? Afterinputtingeachoperand,itmustbeseparatedbyspaces,otherwiseanerrorwillbereported.Whatisthereason?Dotheyhavetobeseparatedbyspaces? I hope these two issues can be clarified in the future learning process.

Development Notes: Java_Calculator 001, supports addition, subtraction, multiplication and division, bracket operations

Preface: This article is compiled by the editor of Programming Notes#. It mainly introduces Java_Calculator 001, which supports addition, subtraction, multiplication and division, and bracket operations. I hope it will be of certain reference value to you. During the summer self-study of Java, I wrote version 001 of calculator in order to practice Java 1 import java.util.ArrayList; 2 //v1.0 supports +-*/, negative sign 3 import java.util.Scanner; 4 public class Caculator_001 5 { 6 public static void main(String[] args) 7 { 8 Scanner input=new Scanner(System.in); 9 double result; 10 String strIn;//=”1+( 2-3*(5+1)/(-4+2*(-6))-3*(+6-(-2)))+6*(5-4)”; 11 System.out.println(“Please enter the formula to be calculated: (no spaces)”); 12 strIn=input.nextLine(); 13 center(strIn); 14 15 input.close(); 16 } 17 18 private static void center(String strIn)//Calculator body 19 { 20 //Verify before calculation 21 judge(strIn); 22 String str0= strIn;//Keep backup 23 strIn=minus(strIn);// Negative sign 24 strIn=plus(strIn);//Positive sign 25 strIn=addBrackets(strIn);//Keep the outermost brackets 26 ArrayList jiSuan1=new ArrayList(); 27 ArrayList jiSuan2=new ArrayList(); 28 ArrayList re=new ArrayList(); 29 ArrayList num=separateNum(strIn);//Separate numbers 30 ArrayList serNum=new ArrayList(); 31 ArrayList sym=separateSym(strIn);//Separation operator 32 serNum=serialNumber(sym);//Statistical numbers 33 double result=0;//Total calculation result 34 int cm=brackets0(sym);//Number of total brackets 35 int bra [][]=new int[cm][2]; 36 bra=brackets(sym);//Bracket statistics 37 38 int m=0;//m maximum number of layers 39 for(int i=0;i<bra.length;i++)…

The sword refers to OfferJZ48, which does not require addition, subtraction, multiplication and division (JavaScript: bit operations)

Time limit:C/C++ 1 second,Other languages ​​2 seconds Space limit:C/C++ 64M,Others Language 128M Popularity Index:250483 Knowledge points of this question: Mathematics Question DescriptionWrite a function,to find the sum of two integers&#xff0c ; It is required that the four arithmetic operators “, -, *, /” must not be used in the function body. Example 1 Input 1,2 Return value 3 Idea: Obtain the uncarried bit through “XOR operation” The previous number A, obtains the carry B, through the “AND operation” and then shifts B to the left by one bit (enlarged by *10),and so on, until B has no carry. Note that operator < function Add(num1 , num2){while(num2!=0){let tmp = num1^num2 //Value without carrynum2 = (num2&num1 )<<1 //Carry valuenum1 = tmp}return num1}

javascript multiplication and addition_Addition, subtraction, multiplication and division in JS

The algorithm of js is inexplicably inaccurate,The following is the universal formula for calculating addition, subtraction, multiplication and division: /** * Addition * @param arg1 * @param arg2 * @returns */ function accAdd(arg1,arg2){ var r1,r2,m; try{r1=arg1.toString().split(“. “)[1].length}catch(e){r1=0}; try{r2=arg2.toString().split(“.”)[1].length }catch(e){r2=0}; m=Math.pow(10,Math.max(r1,r2)); return (arg1 *m+arg2*m)/m; } /** * Subtraction * &#64 ;param arg1 * @param arg2 * @returns */ function accSubtr(arg1 ,arg2){ var r1,r2,m,n; try{r1=arg1.toString().split(“.”)[1].length ;}catch(e){r1=0;} try{r2=arg2.toString().split(“.”)[1].length;}catch(e) {r2=0;} m=Math.pow(10,Math.max(r1,r2)); //Dynamic control of precision length p> n=(r1>=r2)?r1:r2; return ((arg1*m-arg2*m)/m).toFixed(n); } /*** * Multiplication,Get the result value of exact multiplication * @ param arg1 * @param arg2 * @returns */ function accMul(arg1, arg2) { var m=0,s1=arg1.toString(),s2=arg2.toString(); try{m+=s1.split(“.”)[1].length}catch(e){}; try{m+=s2 .split(“.”)[1].length}catch(e){}; return Number(s1.replace(“.”,””))*Number(s2.replace(” .”,””))/Math.pow(10,m); } /*** * Division,Get The result value of exact multiplication * @param arg1 * @param arg2 * @returns */ function accDivCoupon(arg1,arg2){ var t1=0,t2=0,r1,r2; try{t1=arg1.toString().split(“.”)[1].length;}catch(e){} try{t2=arg2.toString() .split(“.”)[1].length;}catch(e){} with(Math){ r1=Number(arg1.toString() .replace(“.”,””)); r2=Number(arg2.toString().replace(“.”,””)); return (r1/r2)*pow(10,t2-t1); } }

Use only ++ operations to implement addition, subtraction, multiplication, and division in PHP

Addition [php] function jiafa($a,$b) { for($i=0;$i<$b;$i++) {          $a++; }   Return $a; } //echo jiafa(4,2); Subtraction [php] function jianfa($a,$b) { $c=0; While($b!=$a) {          $b++;          $c++; }   echo $c; } // end func //jianfa(10,3); Multiplication [php] function chengfa($a,$b) { $c=0; for($j=0;$j<$b;$j++) {           $c=jiafa($c,$a); }   Return $c; } // end func //chengfa(9,3); Division [php] function chufa($d,$e) { $k=0; $f=0; While($f<$d) {          $k++;           $f=chengfa($e,$k);                                                               }   Return $k; } // end func echo chufa(16,2);

PHP’s chr and ord functions implement character addition, subtraction, multiplication and division operations code_PHP tutorial

The chr function is used to convert ASCII codes into charactersord function is used to convert characters into ASCII codes ASCII code is the encoding of characters that the computer can display, and its value range is 0-255. These include punctuation, letters, numbers, Chinese characters, etc. During the programming process, specified characters are often converted into ASCII codes for comparison. The following is a function provided by PHP to convert ASCII codes and characters. 1. chr() function This function is used to convert ASCII code values ​​into strings. The function declaration is as follows: string chr (int ascii); 2. ord() function This function is used to convert a string into an ASCII code value. The function declaration is as follows: int ord(string str); Example: Use the chr() function and ord() function to perform conversion operations between strings and ASCII codes. The program code is as follows: The code is as follows: <?php $str1=chr(88); echo $str1; //The return value is X $str2=chr(ord (X)+1); // echo $str2; //The return value is Y echo “\t”; $str3=ord(‘S’); echo $str3 ; //The return value is 83 ?> Running result: X Y 83 http://www.bkjia.com/PHPjc/324706.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/324706.htmlTechArticlechr function is used to convert ASCII code into characters ord function is…

(PHP implementation) Only use ++ operations to implement addition, subtraction, multiplication, and division_PHP tutorial-php tutorial

Addition The code is as follows: function jiafa($a,$b){ for($i=0;$i<$ b;$i++) { $a++; } return $a;}//echo jiafa(4,2); Subtraction The code is as follows: function jianfa($a,$b){ $c=0; while($b!=$a) { $b++; $c++; } echo $c;} // end func//jianfa (10,3); Multiplication The code is as follows: function chengfa($a,$b){ $c=0; for($j=0;$j<$b;$j++) { $c=jiafa($c,$a); } return $c;} // end func//chengfa(9,3); Division The code is as follows: function chufa($d,$e){ $k=0; $f=0; while($f<$ d) { $k++; $f=chengfa($e,$k); } return $k;} // end funcecho chufa(16,2); http://www.bkjia.com/PHPjc/327885.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327885.htmlTechArticleThe addition code is as follows: function jiafa($a,$b) { for($i=0;$i$b;$i++) { $a++; } return $a; } //echo jiafa(4, 2); The subtraction code is as follows: function jianfa($a,$b) { $…

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索