Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-12-2011, 02:15 PM   PM User | #1
najd1103
New to the CF scene

 
Join Date: Aug 2011
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
najd1103 is an unknown quantity at this point
Re-writing simple calculator to remove errors.

Hi guys i'm new to the world of programming and have been watching some videos on youtube by thenewboston to get to know Java. In one of his tutorials he shows you how to make a basic calculator but i wanted to improve it by adding the option to choose which operation you want to perform. However not being familiar with code i cannot think of a way to put it so that it is error free.I do also have a seperate class file which coresponds with the second paragraph.The errors appear in the if statements because of the characters * / - +. I am currently using eclipse to write my code as it is very useful for showing you your mistakes. Thanks in advance. Code:

Scanner tut7 = new Scanner(System.in);
double fnum, snum, answer;
System.out.println("Enter First Number Please:");
fnum = tut7.nextDouble();
System.out.println("Enter Second Number Please:");
snum = tut7.nextDouble();

Scanner opinput = new Scanner(System.in);
Operation OperationObject = new Operation();
System.out.println("Enter What Operation You Want to Perform: ");
String oper = opinput.nextLine();
OperationObject.calc(oper);

if (oper = * ){
answer = fnum * snum;
System.out.print("The Answer is: ");
System.out.println(answer);
}else{
if (oper = / ){
answer = fnum / snum;
System.out.print("The Answer is: ");
System.out.println(answer);
}else{
if (oper = + ){
answer = fnum + snum;
System.out.print("The Answer is: ");
System.out.println(answer);
}else{
if (oper = - ){

answer = fnum - snum;
System.out.print("The Answer is: ");
System.out.println(answer);
}else{
System.out.println("Invalid Operation!!!");
}
}
}

Last edited by najd1103; 08-12-2011 at 02:42 PM.. Reason: forgot to add something
najd1103 is offline   Reply With Quote
Old 08-12-2011, 06:39 PM   PM User | #2
acomber
New Coder

 
Join Date: Jul 2011
Location: London, UK
Posts: 13
Thanks: 2
Thanked 1 Time in 1 Post
acomber is an unknown quantity at this point
Try posting your question in a Java forum.
acomber is offline   Reply With Quote
Users who have thanked acomber for this post:
najd1103 (08-13-2011)
Old 08-13-2011, 07:58 AM   PM User | #3
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
This is the JavaScript forum. Java and Javascript are entirely different programming languages, in spite of the confusingly similar names. Rather like Austria and Australia!
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
najd1103 (08-13-2011)
Old 08-13-2011, 08:44 AM   PM User | #4
bullant
Banned

 
Join Date: Feb 2011
Posts: 2,699
Thanks: 13
Thanked 395 Times in 395 Posts
bullant is on a distinguished road
Quote:
Originally Posted by najd1103 View Post
...i wanted to improve it by adding the option to choose which operation you want to perform....
You might be able to use JPanel to create a container to store buttons or whatever for the user options.

But in any case, you'll probably get better help in the Java forum. This is a javascript forum.

basically, java is to javascript as ham is to hamster.
bullant is offline   Reply With Quote
Users who have thanked bullant for this post:
najd1103 (08-13-2011)
Old 08-13-2011, 04:40 PM   PM User | #5
najd1103
New to the CF scene

 
Join Date: Aug 2011
Posts: 6
Thanks: 5
Thanked 0 Times in 0 Posts
najd1103 is an unknown quantity at this point
Cheers guys! My mistake.
najd1103 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:05 PM.


Advertisement
Log in to turn off these ads.