public class SwitchCases {

public static void main(String[] args) {
Scanner input = new Scanner (System.in);
System.out.println("Enter two numbers");
int x = input.nextInt();
int y = input.nextInt();

System.out.println("Choose a random number between 1 and 4: ");
int random = input.nextInt();
double result;
switch (random){
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
result = x / y;
break;
default:
System.out.print("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", x, random, y, result);
}
}

why is case 4 giving me an error message?

apparently y=0

The error message in the given code might occur if the user enters a value of `4` for the variable `random` and `y` is `0`. In such a case, you would be attempting to divide `x` by `y`, which leads to a division by zero error.

To fix this error, you can add a condition before the division to check if `y` is not equal to `0`. Here's an updated version of the code:

```java
import java.util.Scanner;

public class SwitchCases {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Enter two numbers");
int x = input.nextInt();
int y = input.nextInt();

System.out.println("Choose a random number between 1 and 4: ");
int random = input.nextInt();
double result;
switch (random) {
case 1:
result = x + y;
break;
case 2:
result = x - y;
break;
case 3:
result = x * y;
break;
case 4:
if (y != 0) {
result = x / y;
} else {
System.out.println("Error! Cannot divide by zero");
return;
}
break;
default:
System.out.print("Error! operator is not correct");
return;
}
System.out.printf("%.1f %c %.1f = %.1f", x, random, y, result);
}
}
```

Now, if the user enters `4` for `random` and `y` is `0`, it will display an error message stating "Error! Cannot divide by zero" instead of encountering a division by zero error.