Advertisement
Guest User

Pre Soňu

a guest
Oct 30th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.71 KB | None | 0 0
  1. package com.jurajmlich;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Main {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         // vypíš
  10.         System.out.println("Welcome to my calculator");
  11.        
  12.         // premenná, ktorá určite či sa kód vo while má opakovať (ak je true, tak áno, ak je false, tak nie)
  13.         Boolean shouldContinueAsking = true;
  14.         while (shouldContinueAsking) {
  15.             System.out.println("State your username");
  16.             // čítaj a ulož do premennej to, čo užívateľ napíše
  17.             String username = scanner.next();
  18.             System.out.println("Insert your password");
  19.             String password = scanner.next();
  20.  
  21.             // ak je meno a heslo správne...
  22.             if (username.equals("sona") && password.equals("sona123")) {
  23.                 // tak sa prestaň pýtať a pokračuj v mojom programe nižšie
  24.                 shouldContinueAsking = false;
  25.             } else {
  26.                 // ak nie je, vypíš chybovú hlášku (a nijako nezmeň či sa má pokračovať pýtať --> opýta sa teda znovu)
  27.                 System.out.println("Username or password incorrect, try again");
  28.             }
  29.         }
  30.        
  31.         // kalkulačka sa bude pýtať donekonečna
  32.         while (true) {
  33.             System.out.println("Insert the first number");
  34.             Double firstNumber = scanner.nextDouble();
  35.             System.out.println("Insert the second number");
  36.             Double secondNumber = scanner.nextDouble();
  37.             System.out.println("Decide which operation you would like me to do. Write the words multiply or divide or add or subtract.. ");
  38.  
  39.             // premenná - zadal užívateľ správnu operáciu?
  40.             Boolean correct = false;
  41.             // kým nezadal správnu operáciu, pýtaj sa ho
  42.             while (!correct) {
  43.                 String operation = scanner.next();
  44.                 if (operation.equals("multiply")) {
  45.                     Double multiplication = firstNumber * secondNumber;
  46.                     System.out.println(multiplication);
  47.                     correct = true;
  48.                 } else if (operation.equals("divide")) {
  49.                     Double division = firstNumber / secondNumber;
  50.                     System.out.println(division);
  51.                     correct = true;
  52.                 } else if (operation.equals("add")) {
  53.                     Double addition = firstNumber + secondNumber;
  54.                     System.out.println(addition);
  55.                     correct = true;
  56.                 } else if (operation.equals("subtract")) {
  57.                     Double subtraction = firstNumber - secondNumber;
  58.                     System.out.println(subtraction);
  59.                     correct = true;
  60.                 } else {
  61.                     System.out.println("You dumb piece of shit");
  62.                     correct = false;
  63.                     System.out.println("Try again");
  64.                 }
  65.             }
  66.             System.out.println("Proceed to the next operation");
  67.  
  68.         }
  69.  
  70.        
  71.  
  72.        
  73.        
  74.        
  75.        
  76.         /*
  77.         // write out a message
  78.         System.out.println("sonicka sa uz trapi");
  79.  
  80.  
  81.         // read from console
  82.         Integer firstNumber = scanner.nextInt();
  83.         Integer secondNumber = scanner.nextInt();
  84.         Integer result = firstNumber * secondNumber;
  85.         String xenon = "ahoj Juri";
  86.        
  87.         if (firstNumber > 10) {
  88.             System.out.println("the number is bigger than 10");
  89.         }
  90.                
  91.         // write the result of 89 + 57
  92.         System.out.println(result);
  93.         System.out.println(xenon);
  94.        
  95.         */
  96.  
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement