How can I return the answer to a calculation where the two numbers and operator used are stored in variables (input from the user).
I am creating a simple calculator and trying to output the answer to "Number" "Operator" "Number2" which can be anything depending on what the user inputs.
int Number;
int Number2;
int Operator;
String OperatorString;
do
{
System.out.println("Enter a number");
Number = UserInputScanner.nextInt();
System.out.println("1. + \n");
System.out.println("2. - \n");
System.out.println("3. * \n");
System.out.println("4. / \n");
Operator = UserInputScanner.nextInt();
switch (Operator) {
case 1: OperatorString = "+";
break;
case 2: OperatorString = "-";
break;
case 3: OperatorString = "*";
break;
case 4: OperatorString = "/";
break;
default: OperatorString = "Invalid Operator";
break;
}
System.out.println(OperatorString);
System.out.println("Enter another number");
Number2 = UserInputScanner.nextInt();
Answer = "Number" + "OperatorString" + "Number2";
System.out.println(Number + OperatorString + Number2 + "=" + Answer);
The output from this when inputting these numbers and operator returns "5+5=5+5" and I am trying to return "5+5=10".
Java provides no built-in way of evaluating a String expression with operators to produce a value of the result, so you would need another switch to compute Answer:
switch (Operator) {
case 1: Answer = Number + Number2;
break;
case 2: Answer = Number - Number2;
break;
case 3: Answer = Number * Number2;
break;
case 4: Answer = Number / Number2;
break;
default: Answer = 0;
break;
}
The structure of the switch is identical to the one that you have before reading the second number, so you can combine the two if you move the reading of Number2 before the switch.
Calculate the answer in Switch block itself
int Number;
int Number2;
int Operator;
String OperatorString;
do
{
System.out.println("Enter a number");
Number = UserInputScanner.nextInt();
System.out.println("1. + \n");
System.out.println("2. - \n");
System.out.println("3. * \n");
System.out.println("4. / \n");
Operator = UserInputScanner.nextInt();
System.out.println("Enter another number");
Number2 = UserInputScanner.nextInt();
switch (Operator) {
case 1: OperatorString = "+";
Answer = Number + Number2;
break;
case 2: OperatorString = "-";
Answer = Number - Number2;
break;
case 3: OperatorString = "*";
Answer = Number * Number2;
break;
case 4: OperatorString = "/";
Answer = Number / Number2;
break;
default: OperatorString = "Invalid Operator";
break;
}
System.out.println(OperatorString);
System.out.println(Number + OperatorString + Number2 + "=" + Answer);
Related
package com.company;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int operator;
double number1, number2, result;
boolean ask = true;
while (ask) {
System.out.println("please select your operator:\n"
+ "1 for +\n" +
"2 for -\n" +
"3 for *\n" +
"4 for %\n" +
"");
operator = myScanner.nextInt();
System.out.println("you chose " + operator + " operator babe");
System.out.println("please enter your first number");
Scanner numberScanner = new Scanner(System.in);
number1 = numberScanner.nextDouble();
System.out.println("please enter your second number");
Scanner numberScanner2 = new Scanner(System.in);
number2 = numberScanner2.nextDouble();
switch (operator) {
case 1:
result = number1 + number2;
System.out.println("result is:" + result);
break;
case 2:
result = number1 - number2;
System.out.println("result is:" + result);
break;
case 3:
result = number1 * number2;
System.out.println("result is:" + result);
break;
case 4:
result = number1 / number2;
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("do yo want to continue?\n" +
"y for yes\n" +
"n for no\n");
char askInput = myScanner.next().charAt(0);
if (askInput=='n') ask=false;
}
}
}
i got trouble in my switch case
if i press any number or letter somthing like 5 or 6 or... it should print you chose wrong operator.
i think problem is in my default but i don't know where is it?
Just reorder your code like this
`public static void main(String[] args) {
Scanner myScanner = new Scanner(System.in);
int operator;
double number1, number2, result;
boolean ask = true;
while (ask) {
System.out.println("please enter your first number");
Scanner numberScanner = new Scanner(System.in);
number1 = numberScanner.nextDouble();
System.out.println("please enter your second number");
Scanner numberScanner2 = new Scanner(System.in);
number2 = numberScanner2.nextDouble();
System.out.println("please select your operator:\n"
+ "1 for +\n"
+ "2 for -\n"
+ "3 for *\n"
+ "4 for %\n"
+ "");
operator = myScanner.nextInt();
switch (operator) {
case 1:
result = number1 + number2;
System.out.println("result is:" + result);
break;
case 2:
result = number1 - number2;
System.out.println("result is:" + result);
break;
case 3:
result = number1 * number2;
System.out.println("result is:" + result);
break;
case 4:
result = number1 / number2;
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("you chose " + operator + " operator babe");
System.out.println("do yo want to continue?\n"
+ "y for yes\n"
+ "n for no\n");
char askInput = myScanner.next().charAt(0);
if (askInput == 'n') {
ask = false;
}
}
}`
and you'll be fine
as for my comment, if you want to validate the input the user does (for the option) before having the user input another 2 numbers, than, yeah you should actually programm it that way that the validation goes RIGHT AFTER the first userinput. HereĀ“s a slightly corrected version of your code.
public static void main(String[] args) {
int operator;
double result;
boolean ask = true;
Scanner numberScanner = new Scanner(System.in);
while (ask) {
System.out.println(
"please select your operator:\n" + "1 for +\n" + "2 for -\n" + "3 for *\n" + "4 for %\n" + "");
operator = numberScanner.nextInt();
System.out.println("you chose " + operator + " operator babe");
// Here was your "Mistake". You instantly started asking the user for another input,
// but actually wanted to ahve the switch statment here
switch (operator) {
case 1:
result = get_num1(numberScanner) + get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 2:
result = get_num1(numberScanner) - get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 3:
result = get_num1(numberScanner) * get_num2(numberScanner);
System.out.println("result is:" + result);
break;
case 4:
result = get_num1(numberScanner) % get_num2(numberScanner);
System.out.println("result is:" + result);
break;
default:
System.out.println("you chosen the wrong operator babe :)");
break;
}
System.out.println("do yo want to continue?\n" + "y for yes\n" + "n for no\n");
char askInput = numberScanner.next().charAt(0);
if (askInput == 'n')
ask = false;
}
}
public static double get_num1(Scanner scanner) {
System.out.println("please enter your first number");
return scanner.nextDouble();
}
public static double get_num2(Scanner scanner) {
System.out.println("please enter your second number");
return scanner.nextDouble();
}
simply you could validate the operator while you assign it with the input.
for example use if condition and check whether its between 1 and 5 and if not print whatever you want
2 things:
you dont need 2 scanners using only one will be enough
the code is behaving so because you go into the switch case AFTER asking the numbers you want to operate...
some condition like:
operator = myScanner.nextInt();
if (operator < 1 || operator > 4) {
}
may help....
I've writter a calculator program in Java, after a user is done with work,
I want this to happen:
i'll ask if he wants to do more operations, if yes, the program should return to choice input. If no, break the program.
What lines should I add to the code? This is my calc program:
import java.util.*;
class calc
{
public static void main(String ar[])
{
char choice;
Scanner in = new Scanner(System.in);
System.out.println("WELCOME TO SHREYDAN'S CALC 1.0");
System.out.println(" ");
System.out.println("YOU CAN DO THE FOLLOWING:");
System.out.println("+: ADDITION");
System.out.println("-: SUBTRACTION");
System.out.println("*: PRODUCT");
System.out.println("/: QUOTIENT");
System.out.println("#: SQUARE ROOT");
System.out.println("^: POWER");
System.out.println("$: ROUND OFF");
System.out.println("!: FACTORIAL");
System.out.println(" ");
System.out.println("ENTER CHOICE");
choice=in.next().charAt(0);
switch(choice)
{
case '+':
System.out.println("ENTER 2 NUMBERS, USER");
double a=in.nextDouble();
double b=in.nextDouble();
System.out.println("SUM = "+(a+b));
break;
case '-':
System.out.println("ENTER 2 NUMBERS, USER");
double c=in.nextDouble();
double d=in.nextDouble();
System.out.println("SUBTRACTING "+d+" FROM "+c+" ... DIFFERENCE = "+(c-d));
break;
case '*':
System.out.println("ENTER 2 NUMBERS, USER");
double e=in.nextDouble();
double f=in.nextDouble();
System.out.println("PRODUCT = "+(e*f));
break;
case '/':
System.out.println("ENTER 2 NUMBERS, USER");
double g=in.nextDouble();
double h=in.nextDouble();
System.out.println("DIVIDING "+g+" BY "+h+" = "+(g/h));
break;
case '#':
System.out.println("ENTER NO. FOR SQAURE ROOT:");
double sqrt=in.nextDouble();
System.out.println("SQUARE ROOT OF "+sqrt+" = "+Math.sqrt(sqrt));
break;
case '^':
System.out.println("ENTER BASE, USER");
double base=in.nextDouble();
System.out.println("ENTER POWER, USER");
double power=in.nextDouble();
System.out.println(base+" RAISED TO POWER "+power+" = "+Math.pow(base,power));
break;
case '$':
System.out.println("ENTER DECIMAL VALUES TO ROUND OFF");
double deci=in.nextDouble();
System.out.println("THE NEAREST ROUND OFF = "+Math.round(deci));
break;
case '!':
System.out.println("ENTER A NO. FOR FACTORIAL:");
int fact=in.nextInt();
int factorial=1;
for(int i=fact; i>=1;i--)
factorial=factorial*i;
System.out.println(fact+"! = "+factorial);
break;
default:
System.out.println("WRONG CHOICE USER");
}
}
}
while loops are your best bet for this type of problem, just think of a condition which the user can choose to toggle the boolean condition.
for example if the user chooses no on the "continuing of operations" choice, then toggle the boolean to false and exit the while loop to end the program.
You need to wrap the program logic in a loop.
Try using a while loop
public static void main(String args[])
{
boolean doContinue = true;
while(doContinue){
char choice;
Scanner in = new Scanner(System.in);
//program logic
//when the user enters a command to end
// set continue=false
}
}
Maybe put the entire program inside a while loop with a continue to run bool condition which could be set false when they want to quit
You can try the following:
import java.util.*;
class calc {
public static void main(String ar[]) {
char choice;
Scanner in = new Scanner(System.in);
System.out.println("WELCOME TO SHREYDAN'S CALC 1.0");
System.out.println(" ");
boolean loop = true;
while (loop) {
System.out.println("YOU CAN DO THE FOLLOWING:");
System.out.println("+: ADDITION");
System.out.println("-: SUBTRACTION");
System.out.println("*: PRODUCT");
System.out.println("/: QUOTIENT");
System.out.println("#: SQUARE ROOT");
System.out.println("^: POWER");
System.out.println("$: ROUND OFF");
System.out.println("!: FACTORIAL");
System.out.println(" ");
System.out.println("ENTER CHOICE");
choice = in.next().charAt(0);
switch (choice) {
case '+':
System.out.println("ENTER 2 NUMBERS, USER");
double a = in.nextDouble();
double b = in.nextDouble();
System.out.println("SUM = " + (a + b));
break;
case '-':
System.out.println("ENTER 2 NUMBERS, USER");
double c = in.nextDouble();
double d = in.nextDouble();
System.out.println("SUBTRACTING " + d + " FROM " + c + " ... DIFFERENCE = " + (c - d));
break;
case '*':
System.out.println("ENTER 2 NUMBERS, USER");
double e = in.nextDouble();
double f = in.nextDouble();
System.out.println("PRODUCT = " + (e * f));
break;
case '/':
System.out.println("ENTER 2 NUMBERS, USER");
double g = in.nextDouble();
double h = in.nextDouble();
System.out.println("DIVIDING " + g + " BY " + h + " = " + (g / h));
break;
case '#':
System.out.println("ENTER NO. FOR SQAURE ROOT:");
double sqrt = in.nextDouble();
System.out.println("SQUARE ROOT OF " + sqrt + " = " + Math.sqrt(sqrt));
break;
case '^':
System.out.println("ENTER BASE, USER");
double base = in.nextDouble();
System.out.println("ENTER POWER, USER");
double power = in.nextDouble();
System.out.println(base + " RAISED TO POWER " + power + " = " + Math.pow(base, power));
break;
case '$':
System.out.println("ENTER DECIMAL VALUES TO ROUND OFF");
double deci = in.nextDouble();
System.out.println("THE NEAREST ROUND OFF = " + Math.round(deci));
break;
case '!':
System.out.println("ENTER A NO. FOR FACTORIAL:");
int fact = in.nextInt();
int factorial = 1;
for (int i = fact; i >= 1; i--)
factorial = factorial * i;
System.out.println(fact + "! = " + factorial);
break;
default:
System.out.println("WRONG CHOICE USER");
}
System.out.println("Want to calculate more?Y/N");
loop = in.next().charAt(0) == 'Y';
}
}
}
I have already researched this question, but could not find an answer that solved my problem. I keep on getting output 0. For this assignment, I'm not allowed to use any methods. In my program, 1+2 equals 0
public static void main(String[] args) {
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
int number = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
while (operation != 5) {
System.out.println("Enter next number");
number = sc.nextInt();
System.out.println("Enter operation");
operation = sc.nextInt();
switch (operation) {
case 1:
result += number;
System.out.println("result= " + result);
break;
case 2:
result -= number;
System.out.println("result= " + result);
break;
case 3:
result *= number;
System.out.println("result= " + result);
break;
case 4:
result/ = number;
System.out.println("result= " + result);
break;
}
System.out.println(result);
}
import java.lang.System;
import java.util.Scanner;
public class Java{
public static void main(String args[])
{
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
result = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
while (operation != 5) {
System.out.println("Enter next number");
int number = sc.nextInt();
switch (operation) {
case 1:
result += number;
System.out.println("result= " + result);
break;
case 2:
result -= number;
System.out.println("result= " + result);
break;
case 3:
result *= number;
System.out.println("result= " + result);
break;
case 4:
result /= number;
System.out.println("result= " + result);
break;
}
System.out.println(result);
System.out.println("Enter operation");
operation = sc.nextInt();
}
}
}
for the first number and second number you are using the variable number..so u will get Zero.
Your first operation was not being executed.
Try this:
public static void main(String[] args) {
int result = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter number");
result = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
while (operation != 5) {
System.out.println("Enter next number");
int operand2 = sc.nextInt();
switch (operation) {
case 1:
result += operand2;
System.out.println("result= " + result);
break;
case 2:
result -= operand2;
System.out.println("result= " + result);
break;
case 3:
result *= operand2;
System.out.println("result= " + result);
break;
case 4:
result /= operand2;
System.out.println("result= " + result);
break;
}
System.out.println("Enter operation");
operation = sc.nextInt();
}
System.out.println("Global result = " + result);
}
}
The problem is that you are reading number and operation twice for the first iteration of the loop instead move the two reading condition after switch case as given below.
public static void main(String[] args) throws java.lang.Exception {
int result = 0;
Scanner sc = new Scanner(System. in );
System.out.println("Enter number");
int number = sc.nextInt();
System.out.println("Enter operation");
System.out.println("1.+");
System.out.println("2.-");
System.out.println("3.*");
System.out.println("4./");
System.out.println("5.=");
int operation = sc.nextInt();
try {
while (operation != 5) {
switch (operation) {
case 1:
result += number;
System.out.println("result= " + result);
break;
case 2:
result -= number;
System.out.println("result= " + result);
break;
case 3:
result *= number;
System.out.println("result= " + result);
break;
case 4:
result /= number;
System.out.println("result= " + result);
break;
}
System.out.println("Enter next number");
number = sc.nextInt();
System.out.println("Enter operation");
operation = sc.nextInt();
}
System.out.println("Final esult is " + result);
} catch (Exception e) {
System.out.println(e);
}
}
Output
Enter number 2
Enter operation 1
1.+
2.-
3.*
4./
5.=
result= 2
Enter next number 5
Enter operation 5
Final result is 2
Demo
Hello I have tried syntax for calculator, with char for operand
but the result always error.
I'm using eclipse (Luna). there are no error or warning message on it.
Syntax
Scanner in = new Scanner(System.in);
int answer = 0;
System.out.println("Enter a number:");
int num1 = in.nextInt();
System.out.println("Enter another number:");
int num2 = in.nextInt();
System.out.println("Enter the operand:");
char input =(char) in.nextInt();
switch (input){
case'*':
answer = num1 * num2;
break;
case '/':
answer = num1/num2;
break;
case '%':
answer = num1%num2;
break;
case '+':
answer = num1 +num2;
break;
case '-':
answer = num1-num2;
break;
default:
System.out.println("Invalid Command");
}
System.out.println("The result is: " + answer);
Debugging
Input number, another number and an operand:
and the error:
InputMismatchException
Just change this line: char input =(char) in.nextInt(); for this char input = in.next().charAt(0)...and you are good to go!
I am just trying to call the methods to the main for each switch when it happens, but i just get the error message everytime i try to call any methods, not trying to return anything. ex. if the user enters a or A i want to call the add method to main
public static void main(String[] args)
{
char character;
Scanner keyboard = new Scanner(System.in);
while (character != 'E' || character != 'e')
{
System.out.println(" A:Addition \n S:Subtraction \n M:Multiplication \n D:Division \n R:Modulus \n E:exit");
switch (character)
{
case 'a':
case 'A':
System.out.println("your choice A");
add();
break;
case 's':
case 'S':
System.out.println("your choice S");
subtraction();
break;
case 'm':
case 'M':
System.out.println("your choice M");
multiplication();
break;
case 'd':
case 'D':
System.out.print("your choice D");
division();
break;
case 'r':
case 'R':
System.out.println("your choice R");
modulus();
break;
default:
System.out.println("Error: please enter a valid letter");
break;
}
}
}
public static void add(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a + b;
System.out.println(a + "plus" + b + "is" + total );
}
public static void subtraction(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a-b;
System.out.println(a + "minus" + b + "is " + total);
}
public static void multiplication(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a*b;
System.out.println(a + "times" + b + "is " + total);
}
public static void division(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total = a/b;
System.out.println(a + "divided" + b + "is " + total);
}
public static void modulus(Scanner keyboard)
{
int a,b;
//get integer 1
System.out.println("enter integer 1");
a = keyboard.nextInt ();
//get integer 2
System.out.println("enter integer 2");
b = keyboard.nextInt();
int total= a%b;
System.out.println(a + "modulus" + b + "is " + total);
System.out.println("The program is terminating");
}
}
you're calling the method but you didn't include an argument
take a look at this.
public static void add(Scanner keyboard)
you have an argument, so you must include an argument when calling this method
so
you must call the method like this.
add(keyboard);
You have defined the method which takes Scanner as argument but you are calling the methods with no args.
All the method you are using are supposed to receive a Scanner object while you pass no argument.
For example you call add(); while it signature is
public static void add(Scanner keyboard)
Which is why you get the error.
Instead, use add(keyboard) and repeat the same for substraction, multiplication, division and modulus methods.
So that your switch would now look like
switch (character) {
case 'a':
case 'A':
System.out.println("your choice A");
add(keyboard);
break;
case 's':
case 'S':
System.out.println("your choice S");
subtraction(keyboard);
break;
case 'm':
case 'M':
System.out.println("your choice M");
multiplication(keyboard);
break;
case 'd':
case 'D':
System.out.print("your choice D");
division(keyboard);
break;
case 'r':
case 'R':
System.out.println("your choice R");
modulus(keyboard);
break;
default:
System.out.println("Error: please enter a valid letter");
break;
}
You are missing the arguments in the method call.
case 'a':
case 'A':
System.out.println("your choice A");
add(keyboard); // Add arguments.
break;