Home > other >  Check if 3 values is between Two numbers in Java
Check if 3 values is between Two numbers in Java

Time:09-22

I ask the user three input which is first, second, third. And I want to count the teenagers among them which is between 20 and 10.

if ((first >10 && first <20)&&
        (second <20 && second>10)&&
        (third<20&&third>10)) {
    System.out.println("All of them are teenagers");

}else if((first <20 && first >10)&&
        (second <20 && second>10)&&
        !(third<20&&third>10)){
    System.out.println("Two of them are teenagers");
}else if((first<20 && first>10)&&
        !(second <20 && second>10)&&
        (third<20&&third>10) ){
    System.out.println("Two of them are teenagers");
}else if (!(first <20 && first >10)&&
        (second <20 &&second>10)&&
        (third<20&&third>10)){
    System.out.println("Two of them are teenagers");
}else if ((first <20 && first >10)&&
        !(second <20 && second>10)&&
        !(third<20&&third>10)){
    System.out.println("One of them is a teenager");
}else if(!(first <20 && first >10)&&
        !(second<20 && second>10)&&
        (third<20&&third>10)){
    System.out.println("One of them is a teenager");
}else if (!(first >10 && first <20)&&
        (second <20 && second>10)&&
        !(third<20&&third>10)){
    System.out.println ("One of them is a teenager");
}else {
    System.out.println("None of them is a teenager");
}
  • When I get output , it's only showing "None of them are teenager" I'm having trouble with this Can you help me pls? What's wrong with my code?*

CodePudding user response:

I'd do something like this:

int teenNb = 0;
int[] arr = {First, Second, Third};
for (int i : arr)
    teenNb  = (i < 20 && i > 10 ? 1 : 0);

switch (teenNb) {
    case 1:
        System.out.println("One of them is a teenager");
        break;
    case 2:
        System.out.println("Two of them are teenagers");
        break;
    case 3:
        System.out.println("All of them are teenagers");
        break;
    default:
        System.out.println("None of them is a teenager");
        break;
}

There may be more efficient or advanced solutions, but this works for me and is fairly simple and easy to understand.

CodePudding user response:

It not easy to find the problem in your code, you can make a better solution by split your problem,

First create one method to check if one user is teenager or not, then user this function for each user and count how many of them are teenagers,

public static boolean isTeenager(int user) {
    return user > 10 && user < 20;
}

public static void main(String[] args) {
    int first = 0, second = 0, third = 0;
    int[] users = {first, second, third};
    int teenagersCount = 0;

    for(int user : users)
        if (isTeenager(user))
            teenagersCount  ;;

    switch (teenagersCount) {
        case 0: System.out.println("None of them are teenagers"); break;
        case 1: System.out.println("One of them is teenager"); break;
        case 2: System.out.println("Two of them are teenagers"); break;
        default: System.out.println("All of them are teenagers"); break;
    }
}

CodePudding user response:

I compiled and ran your code and it works perfectly fine for all cases. So there is nothing wrong with it, except naming conventions, and code repeat. I suspect that if you get the "None of them is a teenagers" all the time, you might not be getting the input correctly from the user.

Alternative solution:

Count the number of teenagers and make a custom result String based on the count. Conveniently, here, you can store the result strings in a list where each result correspond to its index (for more complex cases, you could also use a Map<Integer, String>)

private static final List<String> resultsList = Arrays.asList(
        "None of them is a teenagers",  // index 0
        "One of them is a teenager",    // index 1
        "Two of them are teenagers",    // index 2
        "All of them are teenagers");   // index 3
public static void main(String[] args) {
    int first = 15;
    int second = 9;
    int third = 13;

    int n = countTeenagers(Arrays.asList(first, second, third));
    System.out.println(resultsList.get(n));
}

private static int countTeenagers(List<Integer> ages) {
     return (int)ages.stream()
            .map(age -> age > 10 && age < 20)
            .filter(Boolean::booleanValue)
            .count();
}
  •  Tags:  
  • java
  • Related