Java Learning Simple Calculator--v1.0

Source: Internet
Author: User
Tags what operator

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.