Using Switch Case for Ranges

Asked

Viewed 2,819 times

5

My teacher passed an exercise in which we should use the cases of a switch to treat intervals, he strictly said that we should use the switch and we cannot use if and neither while/do. I’ve tried the following code:

switch(saldo){

        case 0..200:
            //...
        break;
}

However, I know that java does not work that way, I have already researched the internet an equivalent to .. and I didn’t find anything that would solve my problem, there’s how to do it?

Enunciation :

4) A bank shall grant a special variable credit to its customers with the average balance in the last year. Make an algorithm that reads the balance average of a customer and calculate the value of the credit according to the table below. Show a message stating the average balance and the value credit. (use case-of command and do not repeat)

Average balance Percentage

from 0 to 200 no credit

from 201 to 400 20% of the average balance

from 401 to 600 30% of the average balance

above 601 40 % of the average balance

  • These intervals are what ? Time?

  • Do you have the statement of this? I did not understand very well.

  • No, these ranges are integer numbers. For example, if the balance >-1 and <201 I do something, if > 200 is something else, and so on.

1 answer

9


Pure mathematics:

class Main {
    public static void main(String[] args)  {
        int x = 534;
        switch (x / 200) {
            case 0:
                System.out.println("entre 0 e 199");
                break;
            case 1:
                System.out.println("entre 200 e 399");
                break;
            case 2:
                System.out.println("entre 400 e 599");
                break;
            default:
                System.out.println("600 ou mais");
                break;
        }
    }
}

See working on ideone. And in the repl it.. Also put on the Github for future reference.

The original version did not have the statement, but what changes is detail, the secret is to divide by the size of the range.

In fact the statement is impossible, or at least very complex and would have to use another auxiliary mechanism in such a way that the switch will be noise, because not all the intervals are equal, but must be error of the statement, helps the fact to say "above 601" what would make the 601 stay in limbo. You can still do it as long as you create one array assist with possible intervals, or in this case simpler treat the exception of the first case (only it seems to be different).

Something like that:

((x == 0 ? 1 : x) - 1) / 200

Obviously I would need to change the texts I used.

Unless the professor wants you to do this:

switch (x) {
case 0:
case 1:
case 2:
    .
    .
    .
case 200:
    System.out.println("entre 0 e 199");
    break;
case 201:
case 202:
case 203:
    .
    .
    .
case 400:
    System.out.println("entre 200 e 400");
    break;
    .
    .
    .
  • It wouldn’t be every 200?

  • 1

    And it’s every 200. In fact with so poorly worded you don’t know what the real intention is.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.