Results 1 to 5 of 5
- 02-01-2011, 08:57 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
[Question]Simple Calculator problem?
I'm an amateur Java programmer.Started learning, but then had some issues, and proceeded to learn Flash. Flash isn't exactly what I'm looking for, so I'm trying Java again. I have a problem with a calculator I'm attempting to make.
I was wondering if you could help me point out the flaw(s) in my calculator... THe source code is as follows, and it's pretty basic. I just can't get it to display the final output number...
import java.util.Scanner;
public class SimpleCalculator {
public static void main(String[] args) {
// TODO: Input/Output integers - only 1 scanner variable needed
System.out.println("Please enter a number below, can be decimal or whole");
Scanner scan = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
double num1 = scan.nextDouble();
System.out.println("Number one equals "+ num1);
System.out.println("Please enter a second number below");
double num2 = scan.nextDouble();
System.out.println("Number two equals "+ num2);
System.out.println("Please enter an operator for these numbers: +,-,*,/");
String oper = scan2.nextLine();
System.out.println("Your chosen operator is " + oper);
if (oper == "/")
{double num3 = num1/num2;}
else if (oper == "+")
{double num3 = num1+num2;}
else if (oper == "-")
{double num3 =num1 - num2;}
else if (oper == "*")
{double num3 = num1*num2;}
System.out.println("The finalized output for your numbers are: " + num3);
}
}
It always says, in the last line, num3 cannot be resolved to a variable.
I just can't seem to get this to work, I've tried setting double num3 = 0; then running the code, I've also tried removing it completely...
Any help would be gladly appreciated, thanks, Joe.
- 02-01-2011, 09:11 PM #2
Member
- Join Date
- Jan 2011
- Posts
- 6
- Rep Power
- 0
Read about variable visibility and lifetime ;)
Also check how to compare Strings in Java.Last edited by mastal; 02-01-2011 at 09:15 PM.
- 02-01-2011, 09:13 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
Hey,
instead of testing a value with the '=' sign, use the .equals comparison instead. Also declare 'num3' before the if statement.
For example:
double num3 = 0;
if( oper.equals("-" ) )
num3 = num1 - num2;
else if( oper.equals("*" ) )
num3 = num1 * num2;
else if( oper.equals("/" ) )
num3 = num1 / num2;
else
num3 = num1 + num2;
that should give you the correct results.Last edited by ace84; 02-01-2011 at 09:16 PM.
- 02-01-2011, 09:21 PM #4
Member
- Join Date
- Feb 2011
- Posts
- 12
- Rep Power
- 0
@Mastal
Will do, Thanks!
@Ace84
That worked! Thanks! Two hours trying to fix this, solved in a matter of minutes. Thanks again!
~Joe
- 02-02-2011, 02:19 AM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Similar Threads
-
Simple Calculator
By marksey07 in forum New To JavaReplies: 12Last Post: 01-09-2011, 06:30 AM -
Re: Simple Calculator cont'd
By JavaHater in forum New To JavaReplies: 3Last Post: 01-07-2011, 02:00 AM -
creating a simple calculator
By hobo in forum New To JavaReplies: 4Last Post: 11-09-2009, 03:09 AM -
Simple Calculator Display Problem :(
By jimbob in forum Java AppletsReplies: 4Last Post: 07-18-2009, 04:13 AM -
[SOLVED] Simple Conversion Calculator
By dbashby in forum New To JavaReplies: 6Last Post: 03-20-2009, 01:06 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks