Home > Blockchain >  How to compare between two variables in the switch case condition , java
How to compare between two variables in the switch case condition , java

Time:12-06

I want to use switch instead of if In one of my projects, I have two variables , one is user inserted and another is a random one , I'm supposed to have a case for when the two variables are equal, a case when one is bigger and another when it is smaller , but I can't quite write it well , it keeps telling me "boolean cannot be converted to int" any insights on how to solve this problem? (I know it's much easier using if but i need to use switch here) Thanks a lot

import java.util.*;
public class Sheetswitch {
    public static void main (String[] args) {

        Scanner scan = new Scanner (System.in);

        Random rand = new Random ();

        int r = rand.nextInt(9);

        int G = scan.nextInt();

        switch (G) {
            case G==r:
                sout("1")
                break;
            case G>r:
                sout("2")
                break;
            case G<r:
                sout("3")
                break; 
        }
    }
}

CodePudding user response:

You can't check greater then or less then in a switch statement. Switch statements only compare equality between the value and the cases. What's more, at least in older versions of java (not sure about more recent ones), you can't use variables in switch statements, they have to be known at compile time.

if (G == r) {

} else if (G > r) {

} else if (G < r) { // you could also use an else because all other conditions are being hit

}

CodePudding user response:

Like the two commends before allready sugested, switch case will only evaluate the content of one variable. To make your example work you need to take a tiny step in between: Subtract G from r and use the signnum function of the integer class to create a variable which holds the result of your number comparison: 1 == r > G, 0== (r==G) and -1 == r < G Then u can use this result for a switch case scenario.

Import java.util.*;
Public class Sheetswitch {
public static void main (String[] args) {

Scanner scan = new Scanner (System.in);

Random rand = new Random ();

int r = rand.nextInt(9);

int G = scan.nextInt();

int signnum = Integer.signum(r - G);


switch (signnum) {

  case 0:

     sout("1")

     break;

  case 1:

     sout("2")

     break;

  case -1:

     sout("3")

     break; }
}}

CodePudding user response:

trombonedude's answer is the correct one, but if you really want to use switch you can exploit the fact that compareTo from the Comparable interface returns 0, -1 or 1 depending on the result of comparing the object with another number. So for example assuming x is an Integer that has value 2 you can compare it with 4 like this: x.compareTo(4) which returns -1 because the value in x is less than 4.

So back to your problem you can rewrite your code as

import java.util.*;
public class Sheetswitch {
    public static void main (String[] args) {

        Scanner scan = new Scanner (System.in);

        Random rand = new Random ();

        int r = rand.nextInt(9);

        int G = scan.nextInt();

        switch (Integer.valueOf(G).compareTo(r)) {
            case 0:
                System.out.println("1");
                break;
            case 1:
                System.out.println("2");
                break;
            case -1:
                System.out.println("3");
                break; 
        }
    }
}

But, and I can't stress this enough, switch is not the right construct for this kind of things.

This is good only as the solution to a brainteaser.

  • Related