I'm not new to programming per se, I have studied C# for quite a while now, but I haven't really done a lot of exercises by myself, I'm just now starting with java because I want to do Android apps and to test my newly acquired knowledge I wanted to do a basic console calculator, here's what I got so far:
package calculatorSource;
import java.util.*;
public class calculatorJava {
private static Scanner input;
public static void main(String[] args) {
System.out.println("Select an operation");
String choice = null;
input = new Scanner(System.in);
while(choice == null) {
System.out.println("Type 'a' for adition, 's' for subtraction, 'm' for multiplication," + " 'd' for division, or 'mo' for module.");
choice = input.next();
}
while(choice != null) {
if(choice.equals("a")) {
System.out.println(adition(0, 0));
choice = null;
} else if(choice.equals("s")) {
System.out.println(subtraction(0, 0));
choice = null;
} else if(choice.equals("m")) {
System.out.println(multiplication(0, 0));
choice = null;
} else if(choice.equals("d")) {
System.out.println(division(0, 0));
choice = null;
} else if(choice.equals("mo")) {
System.out.println(module(0, 0));
choice = null;
} else {
System.out.println("Option not available, please try again.");
choice = null;
}
}
}
public static float adition(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 + n2;
System.out.println("Result is: " + result);
return adition(result, result);
}
public static float subtraction(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 - n2;
System.out.println("Result is: " + result);
return subtraction(result, result);
}
public static float multiplication(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 * n2;
System.out.println("Result is: " + result);
return multiplication(result, result);
}
public static float division(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 / n2;
System.out.println("Result is: " + result);
return division(result, result);
}
public static float module(float n1, float n2) {
input = new Scanner(System.in);
float result;
System.out.println("Enter first number:");
n1 = input.nextFloat();
System.out.println("Enter second number:");
n2 = input.nextFloat();
result = n1 % n2;
System.out.println("Result is: " + result);
return module(result, result);
}
}
I know it probably is not the best or more efficient calculator out there, but as I said, I just started with java and this is pretty much all I know so far, the program works, as I can make an addition, a division or whatever else I choose, but right after that I want it to give me the option of selecting a different operation, I put the "choice = null" right after the return but it doesn't not seems to be working, I've tried several stuff to this point, but I'm starting to think I may have misunderstood what return actually does, so I thought it would be better to turn to you guys for help.
Any advice is appreciated, thanks!
Instead of
return adition(result, result);
in your adition method, return result.
Otherwise you're just going to repeat the same method until the stack overflows. (Same for other methods also).
Your while (choice != null) { loop isn't necessary, as you always set choice = null;. Since you also don't otherwise change the value of choice in that loop, you may as well just remove the loop.
There is no point in passing parameters into the adition (etc) methods, since you always overwrite them. Just declare them as local variables in those methods:
public static float adition() { // No parameters
// ...
float n1 = input.nextFloat();
// ...
float n2 = input.nextFloat();
You are recursively calling all methods infinitely. choice = null is ok. it will terminate if you will correct your code :
Instead of
MethodaName(result, result);
just write
return result;
Also you need not pass any parameters if taking input from user in method itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With