Click here to Skip to main content
15,888,351 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
import java.util.Scanner;
public class Calculate
{
    public static void main(String[] args)
    {
        int firstNumber,secondNumber,operand;
        Scanner input=new Scanner(System.in);

        //create scanner object to object input from keyboard
        //System.out.println("Pls enter ur choice between 1 to 4 below");
        System.out.println("Enter number between 1 to 4 to choose operand");
        System.out.println("1. ADDITION");
        System.out.println("2. SUBTRACTION");
        System.out.println("3. MULTIPLICATION");
        System.out.println("4. DIVISON");
        operand=input.nextInt();
        System.out.print("Enter the first number:");
        firstNumber=input.nextInt(); //read the first number(firstNumber)
        System.out.print("Enter the second number:");
        secondNumber=input.nextInt();//read the second number(secondNumber)

        switch(operand)
        {
            case 1:
                System.out.println("The sum is = "+(firstNumber+secondNumber));
                break;
            case 2:
                System.out.println("The answer is ="+(firstNumber-secondNumber));
                break;
            case 3:
                System.out.println("The product is ="+(firstNumber*secondNumber));
                break;
            case 4:
                System.out.println("The answer is ="+(firstNumber/secondNumber));
                break;
            default:


        }

    }
}
Posted
Updated 19-Mar-15 4:56am
v2
Comments
TorstenH. 19-Mar-15 10:55am    
What is the problem?
You have "firstNumber" and "secondNumber", now go on with the operand. Same logic.
TorstenH. 19-Mar-15 10:56am    
I added a homework tag, cause this is typical homework...

1 solution

There are several ways to do it. One possible way is to put the whole thing into a while loop. First set a boolean variable, perhaps named continue to true. Then do while a loop on your variable and put everything you have now in that loop.

Then after you compute, you put a message on screen asking the user if they want to do another problem. If they choose No then you set your continue variable = false so that the while loop stops running.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900