+ 1
Calculator (java beginner) - can't find what is wrong
I'm a beginner in programming, now learning Java. Task is to create a calculator, some code is already given. I tried changing different things but it isn't working :-( https://code.sololearn.com/ccpgw0mEIdSB/?ref=app
8 Réponses
+ 3
Anastasia
* remove the package (sololearn does not support it)
* call calculate () inside main
int result = calculate (x, op, y);
System.out.println (x + op + y + " = " + result);
+ 2
your assignment expects you use calculate metod, now you ignore it.
Standard way is move if (op .. ) code part to calculate method() and call it from main() as it is in Denise example, but then instead println, just return value result
+ 1
1)Packages are not supported here so don't mention that in your program.
2)From when does java has started returning 0? remove it.
3)Your whole code should have been inside main() without that function being written. Or else atleast you should have called the function inside the main() with user inputs given in main() and passing them while calling the function.
So kindly look into the code again and rectify the errors.
+ 1
// package assignments; // in Sololearn
...
public static void main( String[] args ){
Scanner ...
System.out.println("Fisrt number: "); // changed to println()
...
int y = scanner.nextInt(); // added type of y
// here call calculate method with parameters and print what it returns
public static int calculate(...
if (op == '+') {
...
} else { // if (op == '^') {
...
// return result * x;
result =* x;
}
return result;
} // added end of if()
// return 0;
...
then you can swap input second num and operator so you will write input
2 ^ 16 ( instead 2 16 ^ )
+ 1
Thank you for your help! Yes, my main () had no code!!!
I changed it, now I can input 3+5 for example but I have no exit code. I think I have a mistake in return :-( I'll try to find this mistake but I would be grateful if you helped me. Thank you in advance!
https://code.sololearn.com/ccpgw0mEIdSB
My code result is:
First number: 1
Operation(+ - * / ^): +
Second number: 3
Process finished with exit code 0
____
package assignments;
import java.util.Scanner;
public class CalculatorApp
{
public static void main( String[] args ){
Scanner scanner = new Scanner(System.in);
System.out.println("First number: ");
int x = scanner.nextInt();
System.out.prinlnt("Operation (+ - * / ^): ");
char op = scanner.next().charAt(0);
System.out.println("Second number: ");
int y = scanner.nextInt();
}
public static int calculate(int x, char op, int y) {
if (op == '+') {
return x + y;
} else if (op == '-') {
return x - y;
}else if (op == '*') {
return x * y;
}else if (op == '/') {
return x / y;
}else op = '^';
int result = 1;
for (int i = 1; i <= y; i++) {
return result * x;
}
return 0;
}
}
+ 1
Anastasia first read my answer and make the changes I have mentioned. Your code is still the same.
+ 1
Thank you guys a lot for your help!!!
Now it works!!!
https://code.sololearn.com/ccpgw0mEIdSB
import java.util.Scanner;
public class CalculatorApp
{
public static void main( String[] args ){
Scanner scanner = new Scanner(System.in);
System.out.print("First number: ");
int x = scanner.nextInt();
System.out.print("Operation (+ - * / ^): ");
char op = scanner.next().charAt(0);
System.out.print("Second number: ");
int y = scanner.nextInt();
if (op == '+') {
System.out.print(x + y);
} else if (op == '-') {
System.out.print(x - y);
}else if (op == '*') {
System.out.print(x * y);
}else if (op == '/') {
System.out.print(x / y);
}else if (op == '^') {
int result = 1;
for (int i=1; i<=y; i++) {
result = result * x;
}
System.out.print(result);
}else
System.out.print("Incorrect input");
}
public static int calculate(int x, char op, int y) {
return 0;
}
}
0
I corrected my code, the task was a bit different, without user's input.
public class CalculatorApp
{
public static void main( String[] args ){
}
public static int calculate(int x, char op, int y) {
if (op == '+') {
return x + y;
} else if (op == '-') {
return x - y;
}else if (op == '*') {
return x * y;
}else if (op == '/') {
return x / y;
}else if (op == '^') {
int result = 1;
for (int i=0; i<y; i++) {
result = result * x;
}
return result;
}
return -1;
}
}