Java Calculator - Need help
Howdy; i'm trying to build a java calculator application; I keep getting errors when i compile; this is the error:
---------------------------------------------------------------------
C:\javap>javac Calc.java
Calc.java:46: variable result might not have been initialized
System.out.println("Answer= " + result);
^
1 error
---------------------------------------------------------------------
and this is the code; mind you i AM a beginners, so any help AT ALL would be great.
--------------------------------------------------------------------
//import Keyboard;
public class Calc
{
public static void main(String[] args)
{
String another = "y";
while(another.equalsIgnoreCase("y"))
{
System.out.println("Enter the first number: ");
int first = Keyboard.readInt();
System.out.println("Enter operator (+, -, * or /): ");
int op = Keyboard.readInt();
System.out.println("Enter second number: ");
int second = Keyboard.readInt();
int result;
switch(op)
{
case '+':
result = first + second;
break;
case '-':
result = first - second;
break;
case '*':
result = first * second;
break;
case '/':
result = first / second;
break;
default:
System.out.println("Wrong operator; use +, -, *, or / functions");
};
System.out.println("Answer= " + result);
System.out.println("Another calculation? (y/n) ");
another = Keyboard.readString();
}
}
}
-----------------------------------------------------------------
basically what it's meant to do is ask the user for a number, then an operator, then a number than give an answer. if the user wants to make a another calculation, s/he pressed "y" and it goes through again.
the problem i have (apart from the error :p ) is that i dont know how to allow for multiple calculations:
eg; 4-3+34=blah or 56*6-12=blah
please help me!