|
-
java problem
Hello; 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 beginner, so any help AT ALL would
be great.
--------------------------------------------------------------------
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 ) is that i dont know how to allow
for multiple calculations:
eg; 4-3+34=blah or 56*6-12=blah
please help me!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks