Results 1 to 8 of 8
Like Tree1Likes
  • 1 Post By DarrylBurke

Thread: Any Improvement Suggestions?

  1. #1
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Any Improvement Suggestions?

    Hi, I just recently made one of my first Java programs, a text-based (non-GUI-based) calculator. It's not really very user-friendly, so I was hoping to get some tips from you guys. Here is my complete code:
    Java Code:
    package calculator;
    
    import java.util.Scanner;
    
    public class Calculator {
        
        public static void main(String[] args) {
            System.out.println("Welcome to Java Calculator!");
            System.out.println("Enter 2 numbers that we will be working with.");
            
            Scanner scan = new Scanner(System.in);
            Scanner scan2 = new Scanner(System.in);
            
            int a = scan.nextInt();
            int b = scan2.nextInt();
            
            System.out.println("Enter the number corresponding to the type of math you would like to solve.");
            System.out.println("1 = Add | 2 = Subtract | 3 = Multiply | 4 = Divide");
            
            Scanner scan3 = new Scanner(System.in);
            
            int dec = scan3.nextInt();
            
            if (dec == 1) {
                System.out.println("Calculating...");
                int ans = a + b;
                System.out.println("Answer = " + ans);
            }
            
            if (dec == 2) {
                System.out.println("Calculating...");
                int ans = a - b;
                System.out.println("Answer = " + ans);
            }
            
            if (dec == 3) {
                System.out.println("Calculating...");
                int ans = a * b;
                System.out.println("Answer = " + ans);
            }
            
            if (dec == 4) {
                System.out.println("Calculating...");
                int ans = a / b;
                System.out.println("Answer = " + ans);
            }
        }
    }
    Any ideas on how to improve this and make it more user-friendly? Thanks in advance!

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Any Improvement Suggestions?

    Quote Originally Posted by Spenser McNeil View Post
    Any Improvement Suggestions?
    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default Re: Any Improvement Suggestions?

    Use else if instead of 4 separate if statements.
    Remove duplicate code from each if statement. Place print statements once before or after.
    Use a single Scanner object. It can be used more than once.
    Maybe have use enter Add, Subtraction etc instead of numbers and use String.equals to determine which calculation to perform.
    Add an extra statement to catch other input and print error message.
    Add a loop so user can do several sums. Make sure to add a "quit" option.

  4. #4
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Any Improvement Suggestions?

    Um, what? From my understanding, the third paragraph states that the title of your thread must reflect the content of the post itself. I think my title fits those parameters.

    Sorry if I came across rude there, but I don't get what you mean.

  5. #5
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default Re: Any Improvement Suggestions?

    Quote Originally Posted by Spenser McNeil View Post
    Um, what? From my understanding, the third paragraph states that the title of your thread must reflect the content of the post itself. I think my title fits those parameters.

    Sorry if I came across rude there, but I don't get what you mean.
    There's nothing descriptive about "Any Improvement Suggestions?"

    If you wish to discuss this further, please start a thread in Feedback/Suggestions.

    db
    Fubarable likes this.
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Any Improvement Suggestions?

    Quote Originally Posted by DarrylBurke View Post
    There's nothing descriptive about "Any Improvement Suggestions?"

    If you wish to discuss this further, please start a thread in Feedback/Suggestions.
    Consider your thread title to be similar to the headline of a newspaper article. It should succinctly on a single line and specifically summarize the content of your question. Your title is succinct but not very specific and is thus not helpful to those searching through subject headings for questions that we consider ourselves to be expert in.

  7. #7
    Join Date
    Jun 2013
    Posts
    5
    Rep Power
    0

    Default Re: Any Improvement Suggestions?

    Quote Originally Posted by DarrylBurke View Post
    There's nothing descriptive about "Any Improvement Suggestions?"

    If you wish to discuss this further, please start a thread in Feedback/Suggestions.

    db
    Ok, ok. You win. I'll try to change the title to something a little more descriptive.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Any Improvement Suggestions?

    If you want a better calculator (one that allows you to type e.g. "(3.4+1.2)*4") read one of my blog articles; it describes (and implements) a parser and evaluator for simple arithmetic expressions (see the link near the top right of this reply).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Any Suggestions???
    By Coulterce in forum Forum Lobby
    Replies: 1
    Last Post: 06-29-2012, 07:38 PM
  2. Suggestions for project
    By Qsc in forum New To Java
    Replies: 3
    Last Post: 11-08-2011, 02:48 PM
  3. Looking for suggestions
    By tonino in forum New To Java
    Replies: 5
    Last Post: 09-26-2010, 08:17 PM
  4. any suggestions?
    By PureAwesomeness in forum New To Java
    Replies: 4
    Last Post: 01-19-2009, 08:34 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •