Demo entry 6770274

calculator

   

Submitted by anonymous on Nov 12, 2018 at 13:13
Language: Java. Code size: 816 Bytes.

package com.somitsoulutions.java.calculator5;

import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		System.out.println("enter add/sub/mul/div and 2 numbers...");
		String sign = in.nextLine();
		int x = in.nextInt();
		int y = in.nextInt();
		
		int result = calculate(sign, x, y);
		
		System.out.println("The result is: " + result);
	
	}
	
	static int calculate(String sign, int x,int y) {
		int result = 0;
		if(sign.equals("add")) {
			result = (x + y) ;
		}
		
		if(sign.equals("sub")) {
             result = (x - y);
	}
		if(sign.equals("mul")) {
			result = (x * y);
		}
		
		if(sign.equals("div")) {
			result = (x / y);
		}
		
		return result;
	}

}

This snippet took 0.00 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).