-
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 ) is that i dont know how to allow for multiple calculations:
eg; 4-3+34=blah or 56*6-12=blah
please help me!
-
int result;
chage this to
int result = 0;
the variable needs to be initialized to something when it's created even though you are changing it's value.
you have to do this when you use variables in if blocks or try blocks or switch blocks and stuff like that
I'm surprised more of you people don't get hit by cars.
-
this is what i got after a few changes
Code:
public class Calculator {
public static void main(String[] args)
{
double result;
do {
System.out.println ("Enter 1st number: "); int number1 = Keyboard.readInt ();
System.out.println ("Enter operator (+, -, * or /): ");
char op = Keyboard.readChar ();
System.out.println ("Enter 2nd number: ");
int number2 = Keyboard.readInt (); result = makeCalc (number1, number2, op);
}
while (result == Double.NaN);
System.out.println ("The result is " + (int) result);
}
private static double makeCalc (int number1, int number2, char op) {
double result;
switch (op)
{
case '+':
result = number1 + number2;
break;
case '-':
result = number1 - number2;
break;
case '*':
result = number1 * number2;
break;
case '/':
result = division(number1, number2);
break;
default:
result = Double.NaN;
break;
}
return result;
}
private static double division (int number1, int number2) {
return (number2 == 0) ?
Double.NaN : number1 / number2;
}private static int division(int number1, int number2){
if(number2 == 0) //testing division
{
System.out.println("Error you can not divide by zero");
return 0;
}
else return(number1/number2);
}}
can someone fix it so the ******* works, i've been trying to get it running all night and im sick of; i need someone to fill in whatever the **** is wrong
-
post the errors your compiler is giving you.
that way we don't have to copy and paste it ourselves and compile it.
I'm surprised more of you people don't get hit by cars.
-
The compiler error was pretty clear about the problem:
Code:
C:\docs\forums\calc_Test>javac Calculator.java
Calculator.java:45: division(int,int) is already defined in Calculator
}private static int division(int number1, int number2){
^
Calculator.java:7: cannot resolve symbol
symbol : variable Keyboard
location: class Calculator
System.out.println ("Enter 1st number: "); int number1 = Keyboard.readInt ();
^
Calculator.java:9: cannot resolve symbol
symbol : variable Keyboard
location: class Calculator
char op = Keyboard.readChar ();
^
Calculator.java:11: cannot resolve symbol
symbol : variable Keyboard
location: class Calculator
int number2 = Keyboard.readInt (); result = makeCalc (number1, number2, op);
^
4 errors
Put simply, you've tried to define two identical methods:
Code:
private static double division (int number1, int number2){
return (number2 == 0) ? Double.NaN : number1 / number2;
}
private static int division(int number1, int number2) {
if(number2 == 0) { //testing division
System.out.println("Error you can not divide by zero");
return 0;
} else {
return(number1/number2);
}
}
(You were also missing a brace).
ArchAngel.
ArchAngel.
O:-)
-
regarding your "multiple calculations" do you want the user to enter it in the form you gave in your example ( eg; 4-3+34=blah or 56*6-12=blah )? you'd need to do some parsing on this. and there's this thing called operator precedence too. it's quite tricky, but not really hard. you can do this through a stack, or that depends on you.
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