Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class OperationsBetweenNumbers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num1 = Integer.parseInt(scanner.nextLine());
- int num2 = Integer.parseInt(scanner.nextLine());
- String symbol = scanner.nextLine();
- String number = "";
- double sum = 0;
- if (symbol.equals("+") || symbol.equals("-") || symbol.equals("*")) {
- if (symbol.equals("+")) {
- sum = num1 + num2;
- } else if (symbol.equals("-")) {
- sum = num1 - num2;
- } else {
- sum = num1 * num2;
- }
- if (sum % 2 == 0) {
- number = "even";
- } else {
- number = "odd";
- }
- System.out.printf("%d %s %d = %d - %s\n", num1, symbol, num2, (int)sum, number);
- } else {
- if (num2 == 0) {
- System.out.printf("Cannot divide %d by zero", num1);
- } else {
- if (symbol.equals("/")) {
- sum = (double) num1 / num2;
- System.out.printf("%d / %d = %.2f\n", num1, num2, sum);
- } else if (symbol.equals("%")) {
- sum = num1 % num2;
- System.out.printf("%d %% %d = %d\n", num1, num2, (int)sum);
- }
- }
- }
- }
- }
- ИЛИ:
- import java.io.PrintStream;
- import java.util.Scanner;
- public class OperationsBetweenNumbers {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int num1 = Integer.parseInt(scanner.nextLine());
- int num2 = Integer.parseInt(scanner.nextLine());
- String symbol = scanner.nextLine();
- double sum =
- symbol.equals("+") ? num1 + num2 :
- symbol.equals("-") ? num1 - num2 :
- symbol.equals("*") ? num1 * num2 :
- symbol.equals("/") && num2 != 0 ? (double) num1 / num2 :
- symbol.equals("%") && num2 != 0 ? num1 % num2 : 0;
- String number = sum % 2 == 0 ? "even" : "odd";
- if ("+-*".contains(symbol)) {
- System.out.printf("%d %s %d = %d - %s\n", num1, symbol, num2, (int)sum, number);
- } else if ("/%".contains(symbol)) {
- PrintStream printStream = num2 == 0
- ? System.out.printf("Cannot divide %d by zero", num1)
- : symbol.equals("/") ? System.out.printf("%d / %d = %.2f\n", num1, num2, sum)
- : System.out.printf("%d %% %d = %d\n", num1, num2, (int) sum);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement