# Thread: Any Improvement Suggestions?

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

## 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. ## Re: Any Improvement Suggestions?

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

db

3. ## 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. Member
Join Date
Jun 2013
Posts
5
Rep Power
0

## 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. ## Re: Any Improvement Suggestions?

Originally Posted by Spenser McNeil
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

6. ## Re: Any Improvement Suggestions?

Originally Posted by DarrylBurke
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. Member
Join Date
Jun 2013
Posts
5
Rep Power
0

## Re: Any Improvement Suggestions?

Originally Posted by DarrylBurke
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. ## 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

#### Posting Permissions

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