Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.68 KB | None | 0 0
  1. /*
  2. * Viikkoharjoitus 6, tehtävä 2.
  3. *
  4. * Lausekielinen ohjelmointi II, syksy 2019.
  5. *
  6. * Kalle Dalgamoni
  7. * 439880
  8. * kalle.dalgamoni@tuni.fi
  9. */
  10.  
  11. import java.util.Scanner;
  12.  
  13. public class Calculator {
  14.    public static final Scanner lukija = new Scanner(System.in);
  15.    /* määritellään komennot */
  16.    public static final String SUMMAA = "add";
  17.    public static final String EROTA = "diff";
  18.    public static final String KERRO = "multi";
  19.    public static final String JAA = "div";
  20.    public static final String LOPETA = "quit";
  21.  
  22.    public static String[] tarkista(String komento) {
  23.       /* luodaan tarkista metodi joka tarkistaa onko syöte komento
  24.       ja onko sillä oikea määrä parametreja*/
  25.       if (komento = null){
  26.          return null;
  27.       }else{      
  28.          String[] komennot = komento.split(" ");
  29.          if  (komennot.length == 3) {
  30.             if (komennot[0].equals(SUMMAA) || komennot[0].equals(EROTA)) {
  31.                return komennot;
  32.             }
  33.             else if (komennot[0].equals(KERRO) || komennot[0].equals(JAA)) {
  34.                return komennot;
  35.             }
  36.          }  
  37.          else {
  38.             return null;
  39.          }      
  40.       }
  41.    }
  42.    
  43.    public static void main(String[] args) {
  44.       /*main metodissa tulostetaan*/
  45.       boolean jatkuu = true;
  46.       System.out.println("Hello! I am a simple calculator.");
  47.       while (jatkuu) {
  48.    
  49.          System.out.println("Please, enter a command:");
  50.          String komento = lukija.nextLine();
  51.  
  52.          if (komento.equals(LOPETA)) {
  53.             jatkuu = false;
  54.          }
  55.          else {
  56.             String[] laske = tarkista(komento);
  57.             if (laske != null) {
  58.                if (laske[0].equals(SUMMAA)) {
  59.                   System.out.println(Integer.parseInt(laske[1]) + Integer.parseInt(laske[2]));
  60.                }
  61.                else if (laske[0].equals(EROTA)) {
  62.                   System.out.println(Integer.parseInt(laske[1]) - Integer.parseInt(laske[2]));
  63.                }
  64.                else if (laske[0].equals(KERRO)) {
  65.                   System.out.println(Integer.parseInt(laske[1]) * Integer.parseInt(laske[2]));
  66.                }
  67.                else if (laske[0].equals(JAA)) {
  68.                   int apu2 = Integer.parseInt(laske[2]);
  69.                   if(apu2 != 0){
  70.                      System.out.println((float)(Double.parseDouble(laske[1]) / Double.parseDouble(laske[2])));
  71.                   }  
  72.                   else{
  73.                      System.out.println("Error!");
  74.                   }
  75.                }
  76.             }
  77.             else {
  78.                System.out.println("Error!");
  79.             }
  80.        
  81.          }  
  82.       }
  83.    }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement