1
Java question
AlieksieiBiespa (0)

Why it works
float x, y;
Scanner input = new Scanner(System.in);
System.out.print("Math operation:");
String mat_op = input.nextLine();
System.out.print("Input num1:");
x = input.nextFloat();
System.out.print("Input num2");
y = input.nextFloat();

    switch (mat_op) {
        case "+": System.out.println("Result = "+(x+y));
        break;
        case "-": System.out.println("Result = "+(x-y));
            break;
        default: System.out.println("Wrong op");
    }

but this not works
float x, y;
Scanner input = new Scanner(System.in);
System.out.print("Input num1:");
x = input.nextFloat();
System.out.print("Input num2");
y = input.nextFloat();
System.out.print("Math operation:");
String mat_op = input.nextLine();

    switch (mat_op) {
        case "+": System.out.println("Result = "+(x+y));
        break;
        case "-": System.out.println("Result = "+(x-y));
            break;
        default: System.out.println("Wrong op");
    }
You are viewing a single comment. View All
Answered by mwilki7 (101) [earned 5 cycles]
View Answer
1
AlieksieiBiespa (0)

swapped
System.out.print("Math operation:");
String mat_op = input.nextLine();