0

I would like help with my java program. I'm trying to compare two different inputs from the user and check if these inputs are between the minimum and maximum age. Also, I need to have two methods in the program and use the boolean method but I got stuck on it. Can someone help me to understand it better and fix it? What I have so far is:

import java.util.Scanner;
class Main {
  public static void main(String[] args) {
    String nome, nomes;
    int ida, idade;
    
    final int MIN_AGE = 18;
    final int MAX_AGE = 65;
    
    Scanner diSexta = new Scanner (System.in);
    System.out.println("Please enter first person's name:");
    nome = diSexta.nextLine();
    System.out.print("Please enter " + nome + "'s age:");
    ida = diSexta.nextInt();
    diSexta.nextLine();
    System.out.println("Please enter second person's name:");
    nomes = diSexta.nextLine();
    System.out.print("Please enter " + nomes + "'s age:");
    idade = diSexta.nextInt();
   
    public static boolean estado() {
    if (ida >= MIN_AGE || ida <= MAX_AGE && idade >= MIN_AGE || idade <= MAX_AGE) {
        return true;
    } else {
        return false;
    }
}
3
  • 2
    "Also, I need to have two methods in the program and use the boolean method but I got stuck on it." - where exactly? Commented Jan 30, 2022 at 0:04
  • a || b && c || d is same as a || (b && c) || d. Also are you sure you need ||? Note that || represents sum or ranges, while && represents intersection. Commented Jan 30, 2022 at 0:09
  • Does this answer your question? Nested functions in Java Commented Jan 30, 2022 at 2:29

2 Answers 2

2

Your current code does not compile. You need to make MIN_AGE and MAX_AGE constants. You also need to move estado() method outside main() method. And then you need to review your estado() implementation. It seems you are looking for something along the following lines:

import java.util.*;

public class Main {

    static final int MIN_AGE = 18;
    static final int MAX_AGE = 65;

    public static void main(String[] args) {
        String nome, nomes;
        int ida, idade;

        Scanner diSexta = new Scanner(System.in);
        System.out.println("Please enter first person's name:");
        nome = diSexta.nextLine();
        System.out.print("Please enter " + nome + "'s age:");
        ida = diSexta.nextInt();
        diSexta.nextLine();
        System.out.println("Please enter second person's name:");
        nomes = diSexta.nextLine();
        System.out.print("Please enter " + nomes + "'s age:");
        idade = diSexta.nextInt();
        System.out.print(estado(ida, idade));
    }

    public static boolean estado(int ida, int idade) {
        return ida >= MIN_AGE && ida <= MAX_AGE && idade >= MIN_AGE && idade <= MAX_AGE;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks man! Also, to substitute the print true that shows and use a if statement answering if (true) System.out.print ("Both are between 18 and 65") and for false Neither are between 18 and 65")
0

Logic error in if statement: All conditions must be evaluated with &&. Because the method returns true when ida variable is greater than MIN_AGE and MAX_AGE. The same applies to the control of the idade variable.

public static boolean estado() 
{
    if((ida >= MIN_AGE && ida <= MAX_AGE) && (idade >= MIN_AGE && idade <= MAX_AGE))
        return true;
    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.