Results 1 to 5 of 5
Thread: Help with a beginning program
- 02-09-2013, 07:44 AM #1
Member
- Join Date
- Feb 2013
- Posts
- 9
- Rep Power
- 0
Help with a beginning program
So I'm trying to just get used to java...and based on what I see we're coming up on next in lecture is just using the operators...so I'm trying to make a basic calculator program that add,sub,div,and mult 2 numbers.
So here's my code...it does compile and run...however when I want to divide, multiply, or subtract...and hit the corresponding s,d,or m...It will only do addition....but the thing is...it does what addition tells it to do...and instead of ending...it goes straight into subtraction.....then multiplication....then division, like there's no stop to the program until all 4 operations finish running. Where am I going wrong?
package egr118home;
import java.util.*;
public class Experimenting {
public static void main(String[] args) {
Scanner input = new Scanner (System.in);
String decision;
System.out.println("What Operation would you like to do? \n"
+ "A for Addition, " + "S for Subtraction, " +
"M for Multiplication," + " D for Division: ");
decision = input.next();
if (decision.equalsIgnoreCase("a"));
{
Addition();
}
if (decision.equalsIgnoreCase("s"));
{
Subtraction();
}
if (decision.equalsIgnoreCase("m"));
{
Multiplication();
}
if (decision.equalsIgnoreCase("d"));
{
Division();
}
}
private static void Addition() {
double aInput1;
double aInput2;
double aAnswer;
Scanner input = new Scanner(System.in);
System.out.println("Enter First Number to be Added: ");
aInput1 = input.nextDouble();
System.out.println("Enter Second Number to be Added: ");
aInput2 = input.nextDouble();
aAnswer = aInput1 + aInput2;
System.out.println("Your answer is: " + aAnswer);
}
private static void Subtraction() {
double sInput1;
double sInput2;
double sAnswer;
Scanner input = new Scanner(System.in);
System.out.println("Enter First Number to be Subtracted: ");
sInput1 = input.nextDouble();
System.out.println("Enter Second Number to be Subtracted: ");
sInput2 = input.nextDouble();
sAnswer = sInput1 - sInput2;
System.out.println("Your answer is: " + sAnswer);
}
private static void Multiplication() {
double mInput1;
double mInput2;
double mAnswer;
Scanner input = new Scanner(System.in);
System.out.println("Enter First Number to be Multiplied: ");
mInput1 = input.nextDouble();
System.out.println("Enter Second Number to be Multiplied: ");
mInput2 = input.nextDouble();
mAnswer = mInput1 * mInput2;
System.out.println("Your answer is: " + mAnswer);
}
private static void Division() {
double dInput1;
double dInput2;
double dAnswer;
Scanner input = new Scanner(System.in);
System.out.println("Enter First Number to be Divided: ");
dInput1 = input.nextDouble();
System.out.println("Enter Second Number to be Divided: ");
dInput2 = input.nextDouble();
dAnswer = dInput1 / dInput2;
System.out.println("Your answer is: " + dAnswer);
}
}
Can anyone help me out and point out where I went wrong?
- 02-09-2013, 09:06 AM #2
Re: Help with a beginning program
Please go through the Forum Rules, particularly the third paragraph. Then go through http://www.java-forums.org/forum-gui...w-members.html and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.
To edit the subject line, click 'Go Advanced' after 'Edit Post'
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-09-2013, 09:09 AM #3
Re: Help with a beginning program
Read up here on how to use if statements: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
Particularly note where there are, and aren't, semicolons.
dbIf you're forever cleaning cobwebs, it's time to get rid of the spiders.
- 02-09-2013, 02:36 PM #4
Member
- Join Date
- Dec 2012
- Location
- Des Moines, IA
- Posts
- 35
- Rep Power
- 0
Re: Help with a beginning program
Darryl is right, check out the if statement tutorials. for sure they need a little work and Welcome!
- 02-12-2013, 03:47 PM #5
Member
- Join Date
- Jan 2013
- Posts
- 22
- Rep Power
- 0
Similar Threads
-
Beginning with Android
By Dark in forum AndroidReplies: 10Last Post: 12-21-2011, 02:53 PM -
Beginning J2ee
By vinci123 in forum Advanced JavaReplies: 1Last Post: 11-21-2011, 10:56 AM -
Beginning advanced java
By vinci123 in forum Advanced JavaReplies: 7Last Post: 09-14-2011, 08:25 AM -
Adding something to beginning of JTextArea
By Huskies in forum AWT / SwingReplies: 4Last Post: 08-30-2011, 01:01 AM -
beginning - IF
By sparkling in forum New To JavaReplies: 26Last Post: 06-05-2011, 05:17 PM
Bookmarks