diff --git a/src/main/java/lessons/lesson02/Main.java b/src/main/java/lessons/lesson02/Main.java new file mode 100644 index 0000000..a75844d --- /dev/null +++ b/src/main/java/lessons/lesson02/Main.java @@ -0,0 +1,55 @@ +package lessons.lesson02; + +import java.util.Scanner; + +public class Main { + public static void main(String[] args) { +// Problems first = new Problems(); +// first.first(); +// +// Problems second = new Problems(); +// second.second(); +// +// Problems third = new Problems(); +// third.third(); +// +// Problems fourth = new Problems(); +// fourth.fourth(); + +// Problems five = new Problems(); +// five.fifth(); +// +// Problems six = new Problems(); +// six.sixth_reverse(); + +// Problems eight = new Problems(); +// eight.eighth(); +// +// Problems nine = new Problems(); +// nine.ninth_simpleNumbers(); + +// Problems2 changer = new Problems2(); +// changer.changeValue(); + +// Problems2 tableOfMult = new Problems2(); +// tableOfMult.tableOfMultiplications(); + +// Problems2 currency = new Problems2(); +// currency.converterOfCurrency(); + +// Problems2 number = new Problems2(); +// number.evenOrOdd(); + +// Problems2 bigger = new Problems2(); +// bigger.checkBigger(); + +// Problems2 cal = new Problems2(); +// cal.calculator(); + +// Problems2 average = new Problems2(); +// average.average(); + + Problems2 search = new Problems2(); + search.search(); + } +} diff --git a/src/main/java/lessons/lesson02/Problems.java b/src/main/java/lessons/lesson02/Problems.java new file mode 100644 index 0000000..4b14e6e --- /dev/null +++ b/src/main/java/lessons/lesson02/Problems.java @@ -0,0 +1,97 @@ +package lessons.lesson02; + +import java.util.Scanner; + +public class Problems { + + void first() { // oneToHundread + for (int i = 1; i <= 100; i++) { + System.out.print(i + " "); + if (i % 10 == 0) { + System.out.println(); + } + } + System.out.println(); + } + + + void second() { // sum_oneTo_N + Scanner inputNumber = new Scanner(System.in); + System.out.println("Input a number: "); + int number = inputNumber.nextInt(); + int res = 0; + for (int i = 1; i <= number; i++) { + res += i; + } + System.out.println(res + "\n"); + } + + void third() { // multiply_one_to_N + Scanner input = new Scanner(System.in); + System.out.println("Input a number: "); + int number = input.nextInt(); + int res = 1; + for (int i = 1; i <= number; i++) { + res *= i; + } + System.out.println(res + "\n"); + } + + void fourth() { // sumOfEvenNumbers + Scanner input = new Scanner(System.in); + System.out.println("Input a number: "); + int number = input.nextInt(); + int res = 0; + for (int i = 1; i <= number; i++) { + if (i % 2 == 0) { + res += i; + } + } + System.out.println(res + "\n"); + } + + void fifth() { // Sum of String numbers + Scanner input = new Scanner(System.in); + System.out.println("Input a number: "); + int number = input.nextInt(); + int res = 0; + while (number > 0) { + res += number % 10; + number = number / 10; + } + System.out.println(res + "\n"); + } + + void sixth_reverse() { // reversing number + Scanner input = new Scanner(System.in); + System.out.println("Input a number: "); + int reverse = 0; + int number = input.nextInt(); + while (number > 0) { + reverse = reverse * 10 + number % 10; + number = number / 10; + } + System.out.println(reverse + "\n"); + } + + + void eighth() { // finds a number divisible by 7 and bigger than 1000 + for (int number = 1000; number < Integer.MAX_VALUE; number++) { + if (number % 7 == 0) { + System.out.println(number); + break; + } + } + } + + void ninth_simpleNumbers() { // Shows only simple numbers + Scanner input = new Scanner(System.in); + System.out.println("Input a number: "); + int number = input.nextInt(); + for (int i = 0; i <= number; i++) { + if (i % 2 != 0) { + System.out.println(i); + } + } + } +} diff --git a/src/main/java/lessons/lesson02/Problems2.java b/src/main/java/lessons/lesson02/Problems2.java new file mode 100644 index 0000000..777b44e --- /dev/null +++ b/src/main/java/lessons/lesson02/Problems2.java @@ -0,0 +1,151 @@ +package lessons.lesson02; + +import java.util.Random; + +public class Problems2 { + + + void changeValue() { + System.out.println("Input two values: "); + int num1 = Utils.input.nextInt(); + int num2 = Utils.input.nextInt(); + System.out.println(num2 + " " + num1); + } + + void tableOfMultiplications() { + System.out.println("Choose operation: \n1:Table of multiplication\n2: The specific raw"); + int numOfoper = Utils.input.nextInt(); + + if (numOfoper == 1) { + for (int i = 1; i <= 9; i++) { + for (int j = 1; j <= 9; j++) { + int res = i * j; + System.out.print(j + " * " + i + " = " + res + "\t"); + } + System.out.println(); + } + } else if (numOfoper == 2) { + System.out.println("Input a number to see a raw of table: "); + int num = Utils.input.nextInt(); + for (int i = 1, j = num; i <= 9; i++) { + int res = j * i; + System.out.println(j + " * " + i + " = " + res); + } + } + } + + void converterOfCurrency() { + int exchange = 12500; + System.out.println("Input the value in $ to convert into UZD: "); + double value = Utils.input.nextDouble(); + System.out.println(exchange * value); + } + + void evenOrOdd() { + System.out.println("Input a number to check: "); + int number = Utils.input.nextInt(); + if (number % 2 == 0) { + System.out.println(number + " is even"); + } else { + System.out.println(number + " is odd"); + } + } + + void checkBigger() { + System.out.println("Print a three number by enter: "); + + int num1 = Utils.input.nextInt(); + int num2 = Utils.input.nextInt(); + int num3 = Utils.input.nextInt(); + + if (num1 > num2 && num1 > num3) { + System.out.println("The biggest number is: " + num1); + } else if (num2 > num1 && num2 > num3) { + System.out.println("The biggest number is: " + num2); + } else { + System.out.println("The biggest number is: " + num3); + } + } + + void calculator() { + System.out.println("Input first number: "); + double num1 = Utils.input.nextDouble(); + + System.out.println("Input second number: "); + double num2 = Utils.input.nextDouble(); + + System.out.println("Input an operation with numbers: (+, -, *, /, %)"); + String operation = Utils.input.next(); + + switch (operation) { + case "+": + System.out.println(num1 + num2); + break; + case "-": + System.out.println(num1 - num2); + break; + case "*": + System.out.println(num1 * num2); + break; + case "/": + System.out.println(num1 / num2); + break; + case "%": + System.out.println(num1 % num2); + break; + default: + System.out.println("Unknown operation! Try again."); + } + } + + void average() { + System.out.print("How many numbers do yo want to write: "); + int n = Utils.input.nextInt(); + System.out.print("Print them: "); + int[] numbers = new int[n]; + int avr = 0; + for (int i = 0; i < n; i++) { + numbers[i] = Utils.input.nextInt(); + } + for (int number : numbers) { + avr += number; + } + System.out.println("Average: " + avr); + } + + void search() { + System.out.print("How many numbers do you want write: "); + int n = Utils.input.nextInt(); + System.out.print("Print an array of numbers: "); + int[] numbers = new int[n]; + for (int i = 0; i < n; i++) { + numbers[i] = Utils.input.nextInt(); + } + System.out.print("Input a number to search it in the array: "); + int search = Utils.input.nextInt(); + boolean found = false; + for (int number : numbers) { + if (search == number) { + found = true; + break; + } + } + if (found) { + System.out.println("The number is found in the array!"); + } else { + System.out.println("No such number! Try again."); + } + } + + void random1() { + Random random = new Random(); + int number = random.nextInt(100); + System.out.println("You have 3 chances to guess right number\nFirst: "); + int try1 = Utils.input.nextInt(); + int try2 = Utils.input.nextInt(); + int try3 = Utils.input.nextInt(); + if (number == try1) { + System.out.println("Found!"); + } + } +} diff --git a/src/main/java/lessons/lesson02/Utils.java b/src/main/java/lessons/lesson02/Utils.java new file mode 100644 index 0000000..429c897 --- /dev/null +++ b/src/main/java/lessons/lesson02/Utils.java @@ -0,0 +1,7 @@ +package lessons.lesson02; + +import java.util.Scanner; + +public class Utils { + public static final Scanner input = new Scanner(System.in); +}