1046 Adder (Java)

Source: Internet
Author: User

Problem description
Enter a string expression that prints the result of the expression containing only addition and subtraction, and the operation is within the integer range.
Input format
Line, string expression. The length of the expression is not more than 100. Expression has at least one item and ends with an equal sign =
Output format
Row, the output results.
Sample input
Sample output


Topic Analysis: Simple computational problems

algorithm analysis: to pay attention to the first number of negative, in addition to the input in Java, to pay attention to the use of methods

Algorithm Design:

Import java.util.*;

Class Main {public
	static void Main (string[] args) {
		Scanner sc = new Scanner (system.in);
		String str = Sc.next ();
		if (Str.charat (0) = = '-') {
			str = ' 0 ' + str;
		int k=0;
		arraylist<character> symbol = new arraylist<character> ();
		arraylist<integer> number = new arraylist<integer> ();
		for (int i=0;i<str.length (); i++) {
			if (Str.charat (i) = = ' + ' | | Str.charat (i) = = '-') {
                number.add (Integer.parseint (str.substring (k, i)));
                Symbol.add (Str.charat (i));
		Number.add (Integer.parseint (str.substring (K,str.length ()-1));
        int Sum=number.get (0);
        for (int i=0;i<symbol.size (); i++) {
            if (symbol.get (i) = = ' + ') {
                sum = number.get (i+1);
            else if (symbol.get (i) = = ') {
                sum-= Number.get (i+1);
        SYSTEM.OUT.PRINTLN (sum);

Related Article

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.