Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Calculator {
- public static final Scanner LUKIJA = new Scanner(System.in);
- private static final String SUMMAA = "add";
- private static final String EROTA = "diff";
- private static final String KERRO = "multi";
- private static final String JAA = "div";
- private static final String LOPETA = "quit";
- public static String[] tarkista(String kasky) {
- String[] kaskyLista = kasky.split(" ");
- if (kaskyLista[0] != null) {
- if (kaskyLista.length == 2) {
- if (kaskyLista[0] == SUMMAA) {
- return kaskyLista;
- }
- else if (kaskyLista[0] == EROTA) {
- return kaskyLista;
- }
- else if (kaskyLista[0] == KERRO) {
- return kaskyLista;
- }
- else if (kaskyLista[0] == JAA) {
- return kaskyLista;
- }
- }
- else if (kaskyLista[0] == LOPETA) {
- return kaskyLista;
- }
- else {
- return null;
- }
- }
- else {
- return null;
- }
- }
- public static void main(String[] args) {
- boolean jatkuu = true;
- while (jatkuu) {
- System.out.println("Hello! I am, a simple calculator.");
- System.out.println("Please, enter a command:");
- String kasky = LUKIJA.nextLine();
- String[] laske = tarkista(kasky);
- if (laske != null) {
- if (laske[0] == SUMMAA) {
- System.out.println(Integer.parseInt(laske[1]) + Integer.parseInt(laske[2]));
- }
- else if (laske[0] == EROTA) {
- System.out.println(Integer.parseInt(laske[1]) - Integer.parseInt(laske[2]));
- }
- else if (laske[0] == KERRO) {
- System.out.println(Integer.parseInt(laske[1]) * Integer.parseInt(laske[2]));
- }
- else if (laske[0] == JAA) {
- System.out.println((float)(Integer.parseInt(laske[1]) % Integer.parseInt(laske[2])));
- }
- else if (laske[0] == LOPETA) {
- jatkuu = false;
- }
- }
- else {
- System.out.println("Error!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement