Професионална програма
Loading...
KaloyanStoyanovvv avatar KaloyanStoyanovvv 66 Точки

Задача Operations Between Numbers ?

Здравейте с този код  https://pastebin.com/4tyvs8Uw  получавам 90/100 и следното съобщение на една от проверките:

The process executing your submission for this test may not have received the output successfully. Please try to submit again the same solution. If the result does not change, then search the error in the submission itself.

Моля някой за малко съдействие, къде бъркам?

0
Programming Basics 23/10/2018 02:41:48
vangel avatar vangel 51 Точки
Best Answer

 

Проверката дали резултатът е нечетно число е грашна.

Твоята проверка е:

 result % 2 ==  1

а трябва да бъде:

result % 2 != 0

 

След корекцията следното решение взима 100/100
 

 import java.util.Scanner;
 
public class OperationsBetweenNumbers {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
 
        double num1 = Double.parseDouble(scanner.nextLine());
        double num2 = Double.parseDouble(scanner.nextLine());
        String symbol = scanner.nextLine();
 
        double result = 0;
        if (symbol.equals("+")) {
            result = num1 + num2;
            if (result % 2 == 0) {
                System.out.printf("%.0f + %.0f = %.0f - even", num1, num2, result);
            } else if (result % 2 != 0) {
                System.out.printf("%.0f + %.0f = %.0f - odd", num1, num2, result);
            }
        } else if (symbol.equals("-")) {
            result = num1 - num2;
            if (result % 2 == 0) {
                System.out.printf("%.0f - %.0f = %.0f - even", num1, num2, result);
            } else if (result % 2 != 0) {
                System.out.printf("%.0f - %.0f = %.0f - odd", num1, num2, result);
            }
        } else if (symbol.equals("*")) {
            result = num1 * num2;
            if (result % 2 == 0) {
                System.out.printf("%.0f * %.0f = %.0f - even", num1, num2, result);
            } else if (result % 2 != 0) {
                System.out.printf("%.0f * %.0f = %.0f - odd", num1, num2, result);
            }
        } else if (symbol.equals("/")) {
            result = num1 / num2;
            String result2 = String.valueOf(result);
            if ((result2.equals("Infinity") || result2.equals("NaN"))) {
                System.out.printf("Cannot divide %.0f by zero", num1);
            } else {
                System.out.printf("%.0f / %.0f = %.2f", num1, num2, result);
            }
        } else if (symbol.equals("%")) {
            result = num1 % num2;
            String result3 = String.valueOf(result);
            if ((result3.equals("Infinity") || result3.equals("NaN"))) {
                System.out.printf("Cannot divide %.0f by zero", num1);
            } else {
                System.out.printf("%.0f %% %.0f = %.0f", num1, num2, result);
            }
        }
    }
}
 

0
KaloyanStoyanovvv avatar KaloyanStoyanovvv 66 Точки

Много благодаря и успех!

0