Very basic calculator using
methods
import java.util.Scanner;
public class Calculator
{
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
System.out.println("Simple Calculator");
System.out.println("\nHere are your options:");
System.out.println("\n1. Addition");
System.out.println("2. Subtraction");
System.out.println("3. Division");
System.out.println("4. Multiplication");
System.out.print("\nWhat would you like to do?: ");
int choice = kb.nextInt();
System.out.println();
if (choice == 1)
{
addition();
}
else if (choice == 2)
{
subtraction();
}
else if (choice == 3)
{
division();
}
else if (choice == 4)
{
multiplication();
}
System.out.println();
kb.close();
}
public static void addition()
{
int nOne, nTwo;
Scanner kb = new Scanner(System.in);
System.out.println("Addition");
System.out.print("\nFirst Number: ");
nOne = kb.nextInt();
System.out.print("\nSecond Number: ");
nTwo = kb.nextInt();
kb.close();
System.out.println("\nSum: " + nOne + " + " + nTwo + " = " + (nOne + nTwo));
}
public static void subtraction()
{
int nOne, nTwo;
Scanner kb = new Scanner(System.in);
System.out.println("Subtraction");
System.out.print("\nFirst Number: ");
nOne = kb.nextInt();
System.out.print("\nSecond Number: ");
nTwo = kb.nextInt();
kb.close();
System.out.println("\nSum: " + nOne + " - " + nTwo + " = " + (nOne - nTwo));
}
public static void division()
{
int nOne, nTwo;
Scanner kb = new Scanner(System.in);
System.out.println("Division");
System.out.print("\nFirst Number: ");
nOne = kb.nextInt();
System.out.print("\nSecond Number: ");
nTwo = kb.nextInt();
kb.close();
System.out.println("\nSum: " + nOne + " / " + nTwo + " = " + (nOne / nTwo));
}
public static void multiplication()
{
int nOne, nTwo;
Scanner kb = new Scanner(System.in);
System.out.println("Multiplication");
System.out.print("\nFirst Number: ");
nOne = kb.nextInt();
System.out.print("\nSecond Number: ");
nTwo = kb.nextInt();
kb.close();
System.out.println("\nSum: " + nOne + " x " + nTwo + " = " + (nOne * nTwo));
}
}
Building a calculator in java with a switch
import java.util.Scanner;
public class Calculator
{
public static void main (String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the first number: > ");
int num1 = scan.nextInt();
System.out.println("Please enter the second number: > ");
int num2 = scan.nextInt();
System.out.println("Please enter the method you would like to use "
+ "(+ , - , * , / , ^ ) > " );
String method = scan.next();
char operation = method.charAt(0);
String output = "";
switch(num1)
{
case 0: output+= "zero";
break;
case 1: output+= " one";
break;
case 2: output+= " two";
break;
case 3: output+= " three";
break;
case 4: output+= " four";
break;
case 5: output+= " five";
break;
case 6: output+= " six";
break;
case 7: output+= " seven";
break;
case 8: output+= " eight";
break;
case 9: output+= " nine";
break;
case 10: output+= " ten";
break;
}
switch (operation)
{
case '+' : output += " plus";
break;
case '-' : output += " minus";
break;
case '*' : output += " times";
break;
case '/' : output += " divided by";
break;
case '^' : output += " to the power of";
break;
}
switch (num2)
{
case 0: output += " zero";
break;
case 1: output += " one";
break;
case 2: output += " two";
break;
case 3: output += " three";
break;
case 4: output += " four";
break;
case 5: output += " five";
break;
case 6: output += " six";
break;
case 7: output += " seven";
break;
case 8: output += " eight";
break;
case 9: output += " nine";
break;
case 10: output += " ten";
break;
}
int answer = 0 ;
switch (answer)
{
case '+' : answer = (answer + (num1 + num2));
break;
case '-' : answer = (answer + (num1 - num2));
break;
case '*' : answer = (answer + (num1 * num2));
break;
case '/' : answer = (answer + (num1 / num2));
break;
case '^' : answer = (answer + (int) (Math.pow(num1,num2) ));
break;
}
System.out.println(output + " equals " + answer );
}
}
No comments:
Post a Comment