Today, I simply tried out Java programming and wrote a short calculator that contained only +,-,*,/. Today, make a brief summary of the calculator.
First, the calculator is divided into input, calculation, output 3 links, the first to introduce the next input link.
Input:
We need to tap 2 numbers to calculate from the keyboard, and an operator for an operation. The calculated number is temporarily set to int integer. The input Class I used here is the scanner class (it is better to understand this analogy bufferedreader). Create an object SC from the scanner class. Then input the integer data by calling the method Sc.nextint () of the object, Sc.next () input string data (XXX in nextxxx () is the data type to be obtained). As a result, you get the data you need.
Calculation:
In the calculation, I need to determine what operator the input operator is, so the resulting operator is compared to the +,-,*,/,4 operator, and the corresponding operation is the same as any of the operators. In this case, I use the string comparison method is Equalsignorecase ("/*4 operator */"), because the method compares the string return is a Boolean value, I just need to judge the result.
Output:
In the output, it is simple to define an integer variable result, and then return the result of the calculation to result. Finally, with System.out.printf ("Result:%d", result), the results are displayed.
There are several issues encountered in writing the code:
1, at the time of input, when I use Sc.nextline () will always skip the input link directly, the next operation of the code. It is understood that the method is related to the delimiter of the current line, and that learning after the specific situation is deepened.
2, in the calculation, when the string comparison, at the beginning of the selection of CompareTo to compare, but the comparison will return an integer number, when 2 are equal to return 0. If you want to use it, it's a little cumbersome to do it further.
Java Learning Simple Calculator--v1.0