Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have a range of numbers in an if/else statement Java

Tags:

java

I'm learning Java through a series of explanations and exercises, and one of them was to create a program that would display a number grade (0-5) in accordance to a number of points (0–29, 30–34, 35–39, 40–44, 45–49, 50–60).

System.out.println("Type the points [0-60]: ");
double points = reader.nextDouble();
reader.nextLine();
if (points < 29) {
    System.out.println("Grade: FAILED.");
} else if (points <= 34) {
    System.out.println("Grade: 1.");
} else if (points <= 39) {
    System.out.println("Grade: 2.");
} else if (points <= 44) {
    System.out.println("Grade: 3.");
} else if (points <= 49) {
    System.out.println("Grade: 4.");
} else if (points >= 50) {
    System.out.println("Grade: 5.");
}

The program works in that it'll give the correct grade because of the overlap in commands, but is there any way to create a range of numbers or strings that could meet the condition of the if/else statement? For example, if the number entered is between 40-44 and so on. Detailed answer would be appreciated since I'm new.

like image 479
Voltar Avatar asked Aug 23 '15 21:08

Voltar


4 Answers

Divide the number by five, and make an array of twelve strings with the textual representation of the grade:

private static final String GRADE_FAILED = "FAILED";
private static final String GRADE_1 = "1";
private static final String GRADE_2 = "2";
private static final String GRADE_3 = "3";
private static final String GRADE_4 = "4";
private static final String GRADE_5 = "5";

private static String GradeStr[] = new {
    GRADE_FAILED // 0..4
,   GRADE_FAILED // 5..9
,   GRADE_FAILED // 10..14
,   GRADE_FAILED // 15..19
,   GRADE_FAILED // 20..24
,   GRADE_FAILED // 25..29
,   GRADE_1      // 30..34
,   GRADE_2      // 35..39
,   GRADE_3      // 40..44
,   GRADE_4      // 45..49
,   GRADE_5      // 50..54
,   GRADE_5      // 55..59
,   GRADE_5      // 60..64 // Only sixty matters
};

System.out.println("Grade:"+GradeStr[points/5]+".");

If you need more granularity, make a bigger array, divide by a smaller number or skip the division altogether, and set the constants in the positions that correspond to the grades that need to be printed. This lets you avoid the conditionals altogether.

Important disclaimer: this approach works best when the number of options is small - up to a hundred or so. When your problem allows for this approach, it is the fastest approach by far. In many cases, it is also the easiest one to read.

like image 104
Sergey Kalinichenko Avatar answered Oct 13 '22 10:10

Sergey Kalinichenko


You can use a NavigableMap, most commonly a TreeMap. The method used below is floorEntry. Quoting Javadoc:

Returns a key-value mapping associated with the greatest key less than or equal to the given key, or null if there is no such key.

Note: Your code was missing an = sign on the 29 boundary, and the points value should be an integer.

Changed to use a grade (0-5), instead of the string used in question.

// Grade boundary is lower-inclusive (grade is 0-60)
TreeMap<Integer, Integer> gradeMap = new TreeMap<>();
gradeMap.put( 0, 0); //  0–29
gradeMap.put(30, 1); // 30–34
gradeMap.put(35, 2); // 35–39
gradeMap.put(40, 3); // 40–44
gradeMap.put(45, 4); // 45–49
gradeMap.put(50, 5); // 50+

System.out.println("Type the points [0-60]: ");
int points = reader.nextInt();
reader.nextLine();
int grade = gradeMap.floorEntry(points).getValue();
System.out.println("Grade: " + grade);
like image 45
Andreas Avatar answered Oct 13 '22 11:10

Andreas


If you want to test if number is between some values use logical operator AND for if statements like:

if(points>=40 && points <=45)

To get more clarity I would suggest you to make a control inversion like this:

    int grade;
    if(points >49){
        grade=5;
    }else if(points >44){
        grade=4;
    }else if(points >39){
        grade=3;
    }else if(points >34){
        grade=2;
    }else if(points >29){
        grade=1;
    }else{
        grade=0;

  }
    System.out.println("Gr: "+grade); //grade=0 = not passed
like image 30
itwasntme Avatar answered Oct 13 '22 09:10

itwasntme


I think you might be looking for

if( points<=44 && points>=40 ) 
like image 20
Stack Player Avatar answered Oct 13 '22 09:10

Stack Player