Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.jurajmlich;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- // vypíš
- System.out.println("Welcome to my calculator");
- // premenná, ktorá určite či sa kód vo while má opakovať (ak je true, tak áno, ak je false, tak nie)
- Boolean shouldContinueAsking = true;
- while (shouldContinueAsking) {
- System.out.println("State your username");
- // čítaj a ulož do premennej to, čo užívateľ napíše
- String username = scanner.next();
- System.out.println("Insert your password");
- String password = scanner.next();
- // ak je meno a heslo správne...
- if (username.equals("sona") && password.equals("sona123")) {
- // tak sa prestaň pýtať a pokračuj v mojom programe nižšie
- shouldContinueAsking = false;
- } else {
- // ak nie je, vypíš chybovú hlášku (a nijako nezmeň či sa má pokračovať pýtať --> opýta sa teda znovu)
- System.out.println("Username or password incorrect, try again");
- }
- }
- // kalkulačka sa bude pýtať donekonečna
- while (true) {
- System.out.println("Insert the first number");
- Double firstNumber = scanner.nextDouble();
- System.out.println("Insert the second number");
- Double secondNumber = scanner.nextDouble();
- System.out.println("Decide which operation you would like me to do. Write the words multiply or divide or add or subtract.. ");
- // premenná - zadal užívateľ správnu operáciu?
- Boolean correct = false;
- // kým nezadal správnu operáciu, pýtaj sa ho
- while (!correct) {
- String operation = scanner.next();
- if (operation.equals("multiply")) {
- Double multiplication = firstNumber * secondNumber;
- System.out.println(multiplication);
- correct = true;
- } else if (operation.equals("divide")) {
- Double division = firstNumber / secondNumber;
- System.out.println(division);
- correct = true;
- } else if (operation.equals("add")) {
- Double addition = firstNumber + secondNumber;
- System.out.println(addition);
- correct = true;
- } else if (operation.equals("subtract")) {
- Double subtraction = firstNumber - secondNumber;
- System.out.println(subtraction);
- correct = true;
- } else {
- System.out.println("You dumb piece of shit");
- correct = false;
- System.out.println("Try again");
- }
- }
- System.out.println("Proceed to the next operation");
- }
- /*
- // write out a message
- System.out.println("sonicka sa uz trapi");
- // read from console
- Integer firstNumber = scanner.nextInt();
- Integer secondNumber = scanner.nextInt();
- Integer result = firstNumber * secondNumber;
- String xenon = "ahoj Juri";
- if (firstNumber > 10) {
- System.out.println("the number is bigger than 10");
- }
- // write the result of 89 + 57
- System.out.println(result);
- System.out.println(xenon);
- */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement