Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package NestedLoops;
- import java.util.Scanner;
- public class Demo {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n1 = Integer.parseInt(scanner.nextLine());
- int n2 = Integer.parseInt(scanner.nextLine());
- char sign = scanner.nextLine().charAt(0);
- double res = 0;
- String evenOrOdd = "";
- switch (sign) {
- case '+':
- res = n1 + n2;
- if (res % 2 == 0) {
- evenOrOdd = "even";
- } else {
- evenOrOdd = "odd";
- }
- System.out.printf("%d %s %d = %.0f - %s", n1, sign, n2, Math.floor(res), evenOrOdd);
- break;
- case '-':
- res = n1 - n2;
- if (res % 2 == 0) {
- evenOrOdd = "even";
- } else {
- evenOrOdd = "odd";
- }
- System.out.printf("%d %s %d = %.0f - %s", n1, sign, n2, Math.floor(res), evenOrOdd);
- //printDefMsg(n1, sign, n2, res, evenOdd(res));
- break;
- case '*':
- res = n1 * n2;
- if (res % 2 == 0) {
- evenOrOdd = "even";
- } else {
- evenOrOdd = "odd";
- }
- System.out.printf("%d %s %d = %.0f - %s", n1, sign, n2, Math.floor(res), evenOrOdd);
- //printDefMsg(n1, sign, n2, res, evenOdd(res));
- break;
- case '/':
- if (n2 == 0) {
- System.out.printf("Cannot divide %d by zero", n1);
- } else {
- res = n1 * 1.0 / n2;
- System.out.printf("%d %s %d = %.2f", n1, sign, n2, res);
- }
- //printZeroMsg(n1);
- break;
- case '%':
- if (n2 == 0) {
- System.out.printf("Cannot divide %d by zero", n1);
- } else {
- res = n1 % n2;
- System.out.printf("%d %s %d = %.0f", n1, sign, n2, Math.floor(res));
- }
- break;
- default:
- break;
- }
- }
- // private static String evenOdd(int number){
- // String evenOrOdd = "even";
- // if (number % 2 == 1){
- // evenOrOdd = "odd";
- // }
- // return evenOrOdd;
- // }
- // private static void printZeroMsg(int n1){
- // System.out.printf("Cannot divide %d by zero ", n1);
- // }
- //
- // private static void printDefMsg(int n1, char sign, int n2, int res, String evenOdd){
- // System.out.printf("%d %c %d = %d - %s", n1, sign, n2, res, evenOdd);
- // }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement